<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;




//Load Composer's autoloader


// OneThink常量定义
// const ONETHINK_VERSION = '1.1.141101';
const ONETHINK_ADDON_PATH  = './Addons/'; // 微信插件
const ONETHINK_PLUGIN_PATH = './Plugins/'; // 系统插件
/**
 * 系统公共库文件
 * 主要定义系统公共函数库
 */

/**
 * 检测用户是否登录
 *
 * @return integer 0-未登录，大于0-当前登录用户ID
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function is_login()
{
    $user = session('user_auth');
    if (empty($user)) {
        return 0;
    } else {
        return session('user_auth_sign') == data_auth_sign($user) ? $user['uid'] : 0;
    }
}

/**
 * 检测当前用户是否为管理员
 *
 * @return boolean true-管理员，false-非管理员
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function is_administrator($uid = null)
{
    $uid = is_null($uid) ? is_login() : $uid;
    return $uid && (intval($uid) === C('USER_ADMINISTRATOR'));
}

/**
 * 字符串转换为数组，主要用于把分隔符调整到第二个参数
 *
 * @param string $str
 *            要分割的字符串
 * @param string $glue
 *            分割符
 * @return array
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function str2arr($str, $glue = ',')
{
    return explode($glue, $str);
}

/**
 * 数组转换为字符串，主要用于把分隔符调整到第二个参数
 *
 * @param array $arr
 *            要连接的数组
 * @param string $glue
 *            分割符
 * @return string
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */

function arr2str($arr = array(), $glue = ',')
{
    if (!count($arr)) {
        return '';
    }

    return implode($glue, $arr);
}

/**
 * 字符串截取，支持中文和其他编码
 *
 * @access public
 * @param string $str
 *            需要转换的字符串
 * @param string $start
 *            开始位置
 * @param string $length
 *            截取长度
 * @param string $charset
 *            编码格式
 * @param string $suffix
 *            截断显示字符
 * @return string
 */
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
{
    if (function_exists("mb_substr")) {
        $slice = mb_substr($str, $start, $length, $charset);
    } elseif (function_exists('iconv_substr')) {
        $slice = iconv_substr($str, $start, $length, $charset);
        if (false === $slice) {
            $slice = '';
        }
    } else {
        $re['utf-8']  = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("", array_slice($match[0], $start, $length));
    }

    return $suffix && $str != $slice ? $slice . '...' : $slice;
}
/**
 * 方法增强，根据$length自动判断是否应该显示...
 * 字符串截取，支持中文和其他编码
 * QQ:125682133
 *
 * @access public
 * @param string $str
 *            需要转换的字符串
 * @param string $start
 *            开始位置
 * @param string $length
 *            截取长度
 * @param string $charset
 *            编码格式
 * @param string $suffix
 *            截断显示字符
 * @return string
 */
function msubstr_local($str, $start = 0, $length, $charset = "utf-8")
{
    if (function_exists("mb_substr")) {
        $slice = mb_substr($str, $start, $length, $charset);
    } elseif (function_exists('iconv_substr')) {
        $slice = iconv_substr($str, $start, $length, $charset);
        if (false === $slice) {
            $slice = '';
        }
    } else {
        $re['utf-8']  = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);

        $slice = join("", array_slice($match[0], $start, $length));
    }
    return (strlen($str) > strlen($slice)) ? $slice . '...' : $slice;
}
/**
 * 系统加密方法
 *
 * @param string $data
 *            要加密的字符串
 * @param string $key
 *            加密密钥
 * @param int $expire
 *            过期时间 单位 秒
 * @return string
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function think_encrypt($data, $key = '', $expire = 0)
{
    $key = md5(empty($key) ? C('DATA_AUTH_KEY') : $key);

    $data = base64_encode($data);
    $x    = 0;
    $len  = strlen($data);
    $l    = strlen($key);
    $char = '';

    for ($i = 0; $i < $len; $i++) {
        if ($x == $l) {
            $x = 0;
        }

        $char .= substr($key, $x, 1);
        $x++;
    }

    $str = sprintf('%010d', $expire ? $expire + time() : 0);

    for ($i = 0; $i < $len; $i++) {
        $str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1))) % 256);
    }
    return str_replace(array(
        '+',
        '/',
        '=',
    ), array(
        '-',
        '_',
        '',
    ), base64_encode($str));
}

/**
 * 系统解密方法
 *
 * @param string $data
 *            要解密的字符串 （必须是think_encrypt方法加密的字符串）
 * @param string $key
 *            加密密钥
 * @return string
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function think_decrypt($data, $key = '')
{
    $key  = md5(empty($key) ? C('DATA_AUTH_KEY') : $key);
    $data = str_replace(array(
        '-',
        '_',
    ), array(
        '+',
        '/',
    ), $data);
    $mod4 = strlen($data) % 4;
    if ($mod4) {
        $data .= substr('====', $mod4);
    }
    $data   = base64_decode($data);
    $expire = substr($data, 0, 10);
    $data   = substr($data, 10);

    if ($expire > 0 && $expire < time()) {
        return '';
    }
    $x    = 0;
    $len  = strlen($data);
    $l    = strlen($key);
    $char = $str = '';

    for ($i = 0; $i < $len; $i++) {
        if ($x == $l) {
            $x = 0;
        }

        $char .= substr($key, $x, 1);
        $x++;
    }

    for ($i = 0; $i < $len; $i++) {
        if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
            $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
        } else {
            $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
        }
    }
    return base64_decode($str);
}

/**
 * 数据签名认证
 *
 * @param array $data
 *            被认证的数据
 * @return string 签名
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function data_auth_sign($data)
{
    // 数据类型检测
    if (!is_array($data)) {
        $data = (array) $data;
    }
    ksort($data); // 排序
    $code = http_build_query($data); // url编码并生成query字符串
    $sign = sha1($code); // 生成签名
    return $sign;
}

/**
 * 对查询结果集进行排序
 *
 * @access public
 * @param array $list
 *            查询结果
 * @param string $field
 *            排序的字段名
 * @param array $sortby
 *            排序类型
 *            asc正向排序 desc逆向排序 nat自然排序
 * @return array
 *
 */
function list_sort_by($list, $field, $sortby = 'asc')
{
    if (is_array($list)) {
        $refer = $resultSet = array();
        foreach ($list as $i => $data) {
            $refer[$i] = &$data[$field];
        }

        switch ($sortby) {
            case 'asc': // 正向排序
                asort($refer);
                break;
            case 'desc': // 逆向排序
                arsort($refer);
                break;
            case 'nat': // 自然排序
                natcasesort($refer);
                break;
        }
        foreach ($refer as $key => $val) {
            $resultSet[] = &$list[$key];
        }

        return $resultSet;
    }
    return false;
}

/**
 * 把返回的数据集转换成Tree
 *
 * @param array $list
 *            要转换的数据集
 * @param string $pid
 *            parent标记字段
 * @param string $level
 *            level标记字段
 * @return array
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
{
    // 创建Tree
    $tree = array();
    if (is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] = &$list[$key];
        }
        foreach ($list as $key => $data) {
            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] = &$list[$key];
            } else {
                if (isset($refer[$parentId])) {
                    $parent           = &$refer[$parentId];
                    $parent[$child][] = &$list[$key];
                }
            }
        }
    }
    return $tree;
}

/**
 * 将list_to_tree的树还原成列表
 *
 * @param array $tree
 *            原来的树
 * @param string $child
 *            孩子节点的键
 * @param string $order
 *            排序显示的键，一般是主键 升序排列
 * @param array $list
 *            过渡用的中间数组，
 * @return array 返回排过序的列表数组
 * @author yangweijie <yangweijiester@gmail.com>
 */
function tree_to_list($tree, $child = '_child', $order = 'id', &$list = array())
{
    if (is_array($tree)) {
        $refer = array();
        foreach ($tree as $key => $value) {
            $reffer = $value;
            if (isset($reffer[$child])) {
                unset($reffer[$child]);
                tree_to_list($value[$child], $child, $order, $list);
            }
            $list[] = $reffer;
        }
        $list = list_sort_by($list, $order, $sortby = 'asc');
    }
    return $list;
}
/**
 * 树形列表
 *
 * @param array $list
 *            数据库原始数据
 * @param array $res_list
 *            返回的结果数组
 * @param int $pid
 *            上级ID
 * @param int $level
 *            当前处理的层级
 */
function list_tree($list, &$res_list, $pid = 0, $level = 0)
{
    foreach ($list as $k => $v) {
        if (intval($v['pid']) != $pid) {
            continue;
        }

        if ($level > 0) {
            $space = '';
            for ($i = 1; $i < $level; $i++) {
                $space .= '──';
            }
            $v['title'] = '├──' . $space . $v['title'];
        }

        $v['level'] = $level;
        $res_list[] = $v;
        unset($list[$k]);

        list_tree($list, $res_list, $v['id'], $level + 1);
    }
}
/**
 * 格式化字节大小
 *
 * @param number $size
 *            字节数
 * @param string $delimiter
 *            数字和单位分隔符
 * @return string 格式化后的带单位的大小
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function format_bytes($size, $delimiter = '')
{
    $units = array(
        'B',
        'KB',
        'MB',
        'GB',
        'TB',
        'PB',
    );
    for ($i = 0; $size >= 1024 && $i < 5; $i++) {
        $size /= 1024;
    }

    return round($size, 2) . $delimiter . $units[$i];
}

/**
 * 设置跳转页面URL
 * 使用函数再次封装，方便以后选择不同的存储方式（目前使用cookie存储）
 *
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function set_redirect_url($url)
{
    cookie('redirect_url', $url);
}

/**
 * 获取跳转页面URL
 *
 * @return string 跳转页URL
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_redirect_url()
{
    $url = cookie('redirect_url');
    return empty($url) ? __APP__ : $url;
}

/**
 * 处理插件钩子
 *
 * @param string $hook
 *            钩子名称
 * @param mixed $params
 *            传入参数
 * @return void
 */
function hook($hook, $params = array())
{
    \Think\Hook::listen($hook, $params);
}

/**
 * 获取插件类的类名
 *
 * @param strng $name
 *            插件名
 */
function get_addon_class($name)
{
    $class = "Addons\\{$name}\\{$name}Addon";
    if (!class_exists($class)) {
        $class = "Plugins\\{$name}\\{$name}Addon";
    }
    return $class;
}

/**
 * 获取插件类的配置文件数组
 *
 * @param string $name
 *            插件名
 */
function get_addon_config($name)
{
    $class = get_addon_class($name);
    if (class_exists($class)) {
        $addon = new $class();
        return $addon->getConfig();
    } else {
        return array();
    }
}

/**
 * 插件显示内容里生成访问插件的url
 *
 * @param string $url
 *            url
 * @param array $param
 *            参数
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function addons_url($url, $param = array())
{
    // 凡星：修复如user_center://user_center/add 识别错误的问题
    $urlArr = explode('://', $url);
    if (stripos($urlArr[0], '_') !== false) {
        $addons = $urlArr[0];
        $url    = HTTP_PREFIX . $urlArr[1];
    }
    $url                       = parse_url($url);
    $case                      = C('URL_CASE_INSENSITIVE');
    !$addons || $url['scheme'] = $addons;
    $addons                    = $case ? parse_name($url['scheme']) : $url['scheme'];
    $controller                = $case ? parse_name($url['host']) : $url['host'];
    $action                    = trim($case ? strtolower($url['path']) : $url['path'], '/');

    /* 解析URL带的参数 */
    if (isset($url['query'])) {
        parse_str($url['query'], $query);
        $param = array_merge($query, $param);
    }

    /* 基础参数 */
    $params = array(
        '_addons'     => ucfirst($addons),
        '_controller' => ucfirst($controller),
        '_action'     => $action,
    );
    $params = array_merge($params, $param); // 添加额外参数

    $qurl = is_dir(ONETHINK_PLUGIN_PATH . $params['_addons']) ? "Home/Addons/plugin" : "Home/Addons/execute";

    return U($qurl, $params);
}

/**
 * 时间戳格式化
 *
 * @param int $time
 * @return string 完整的时间显示
 * @author huajie <banhuajie@163.com>
 */
function time_format($time = null, $format = 'Y-m-d H:i')
{
    if (empty($time)) {
        return '';
    }

    $time = $time === null ? NOW_TIME : intval($time);
    return date($format, $time);
}
function day_format($time = null)
{
    return time_format($time, 'Y-m-d');
}
function hour_format($time = null)
{
    return time_format($time, 'H:i');
}
function time_offset($time = null)
{
    if (empty($time)) {
        return '00:00';
    }

    $mod = $time % 60;
    $min = ($time - $mod) / 60;

    $mod < 10 && $mod = '0' . $mod;
    $min < 10 && $min = '0' . $min;

    return $min . ':' . $mod;
}
// 获取用户信息
function getUserInfo($uid, $field = '')
{
    $info = D('Common/User')->getUserInfo($uid);
    // dump ( $info );
    return empty($field) ? $info : $info[$field];
}
/**
 * 根据用户ID获取用户名
 *
 * @param integer $uid
 *            用户ID
 * @return string 用户名
 */
function get_username($uid = 0)
{
    return get_nickname($uid);
}
function get_userface($uid = 0)
{
    return getUserInfo($uid, $field = 'headimgurl');
}
/**
 * 以下几个获取用户信息都是兼容旧系统的写法
 */
function get_nickname($uid = 0)
{
    if (empty($uid)) {
        return '';
    }

    return getUserInfo($uid, $field = 'nickname');
}
function get_truename($uid)
{
    return getUserInfo($uid, $field = 'truename');
}
function get_userinfo($uid, $field = '')
{
    return getUserInfo($uid, $field);
}

// 获取店铺信息
function get_shopinfo($shopid, $field = '')
{
    $info = D('Addons://Shop/Shop')->getShopInfo($shopid, true);
    // dump ( $info );
    return empty($field) ? $info : $info[$field];
}

function get_followinfo($id, $field = '')
{
    return getUserInfo($id, $field);
}
function get_mult_userinfo($uid)
{
    return getUserInfo($uid);
}
function get_mult_username($uids)
{
    is_array($uids) || $uids = explode(',', $uids);

    $uids = array_filter($uids);
    if (empty($uids)) {
        return;
    }

    foreach ($uids as $uid) {
        $name = get_truename($uid);
        if ($name) {
            $nameArr[] = $name;
        }
    }

    return implode(', ', $nameArr);
}

/**
 * 获取分类信息并缓存分类
 *
 * @param integer $id
 *            分类ID
 * @param string $field
 *            要获取的字段名
 * @return string 分类信息
 */
function get_category($id, $field = null)
{
    static $list;

    /* 非法分类ID */
    if (empty($id) || !is_numeric($id)) {
        return '';
    }

    /* 读取缓存数据 */
    if (empty($list)) {
        $list = S('sys_category_list');
    }

    /* 获取分类名称 */
    if (!isset($list[$id])) {
        $cate = M('Category')->find($id);
        if (!$cate || 1 != $cate['status']) {
            // 不存在分类，或分类被禁用
            return '';
        }
        $list[$id] = $cate;
        S('sys_category_list', $list); // 更新缓存
    }
    return is_null($field) ? $list[$id] : $list[$id][$field];
}

/* 根据ID获取分类标识 */
function get_category_name($id)
{
    return get_category($id, 'name');
}

/* 根据ID获取分类名称 */
function get_category_title($id)
{
    return get_category($id, 'title');
}

/**
 * 获取顶级模型信息
 */
function get_top_model($model_id = null)
{
    $map['status'] = 1;
    if (!is_null($model_id)) {
        $map['id'] = array(
            'neq',
            $model_id,
        );
    }
    $model = M('model')->where($map)->field(true)->select();
    foreach ($model as $value) {
        $list[$value['id']] = $value;
    }
    return $list;
}

/**
 * 解析UBB数据
 *
 * @param string $data
 *            UBB字符串
 * @return string 解析为HTML的数据
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function ubb($data)
{
    // TODO: 待完善，目前返回原始数据
    return $data;
}

/**
 * 记录行为日志，并执行该行为的规则
 *
 * @param string $action
 *            行为标识
 * @param string $model
 *            触发行为的模型名
 * @param int $record_id
 *            触发行为的记录id
 * @param int $user_id
 *            执行行为的用户id
 * @return boolean
 * @author huajie <banhuajie@163.com>
 */
function action_log($action = null, $model = null, $record_id = null, $user_id = null)
{

    // 参数检查
    if (empty($action) || empty($model) || empty($record_id)) {
        return '参数不能为空';
    }
    if (empty($user_id)) {
        $user_id = is_login();
    }

    // 查询行为,判断是否执行
    $action_info = M('Action')->getByName($action);
    if ($action_info['status'] != 1) {
        return '该行为被禁用或删除';
    }

    // 插入行为日志
    $data['action_id']   = $action_info['id'];
    $data['user_id']     = $user_id;
    $data['action_ip']   = ip2long(get_client_ip());
    $data['model']       = $model;
    $data['record_id']   = $record_id;
    $data['create_time'] = NOW_TIME;

    // 解析日志规则,生成日志备注
    if (!empty($action_info['log'])) {
        if (preg_match_all('/\[(\S+?)\]/', $action_info['log'], $match)) {
            $log['user']   = $user_id;
            $log['record'] = $record_id;
            $log['model']  = $model;
            $log['time']   = NOW_TIME;
            $log['data']   = array(
                'user'   => $user_id,
                'model'  => $model,
                'record' => $record_id,
                'time'   => NOW_TIME,
            );
            foreach ($match[1] as $value) {
                $param = explode('|', $value);
                if (isset($param[1])) {
                    $replace[] = call_user_func($param[1], $log[$param[0]]);
                } else {
                    $replace[] = $log[$param[0]];
                }
            }
            $data['remark'] = str_replace($match[0], $replace, $action_info['log']);
        } else {
            $data['remark'] = $action_info['log'];
        }
    } else {
        // 未定义日志规则，记录操作url
        $data['remark'] = '操作url：' . $_SERVER['REQUEST_URI'];
    }

    M('ActionLog')->add($data);

    if (!empty($action_info['rule'])) {
        // 解析行为
        $rules = parse_action($action, $user_id);

        // 执行行为
        $res = execute_action($rules, $action_info['id'], $user_id);
    }
}

/**
 * 解析行为规则
 * 规则定义 table:$table|field:$field|condition:$condition|rule:$rule[|cycle:$cycle|max:$max][;......]
 * 规则字段解释：table->要操作的数据表，不需要加表前缀；
 * field->要操作的字段；
 * condition->操作的条件，目前支持字符串，默认变量{$self}为执行行为的用户
 * rule->对字段进行的具体操作，目前支持四则混合运算，如：1+score*2/2-3
 * cycle->执行周期，单位（小时），表示$cycle小时内最多执行$max次
 * max->单个周期内的最大执行次数（$cycle和$max必须同时定义，否则无效）
 * 单个行为后可加 ； 连接其他规则
 *
 * @param string $action
 *            行为id或者name
 * @param int $self
 *            替换规则里的变量为执行用户的id
 * @return boolean array: ， 成功返回规则数组
 * @author huajie <banhuajie@163.com>
 */
function parse_action($action = null, $self)
{
    if (empty($action)) {
        return false;
    }

    // 参数支持id或者name
    if (is_numeric($action)) {
        $map = array(
            'id' => $action,
        );
    } else {
        $map = array(
            'name' => $action,
        );
    }

    // 查询行为信息
    $info = M('Action')->where($map)->find();
    if (!$info || $info['status'] != 1) {
        return false;
    }

    // 解析规则:table:$table|field:$field|condition:$condition|rule:$rule[|cycle:$cycle|max:$max][;......]
    $rules  = $info['rule'];
    $rules  = str_replace('{$self}', $self, $rules);
    $rules  = explode(';', $rules);
    $return = array();
    foreach ($rules as $key => &$rule) {
        $rule = explode('|', $rule);
        foreach ($rule as $k => $fields) {
            $field = empty($fields) ? array() : explode(':', $fields);
            if (!empty($field)) {
                $return[$key][$field[0]] = $field[1];
            }
        }
        // cycle(检查周期)和max(周期内最大执行次数)必须同时存在，否则去掉这两个条件
        if (!array_key_exists('cycle', $return[$key]) || !array_key_exists('max', $return[$key])) {
            unset($return[$key]['cycle'], $return[$key]['max']);
        }
    }

    return $return;
}

/**
 * 执行行为
 *
 * @param array $rules
 *            解析后的规则数组
 * @param int $action_id
 *            行为id
 * @param array $user_id
 *            执行的用户id
 * @return boolean false 失败 ， true 成功
 * @author huajie <banhuajie@163.com>
 */
function execute_action($rules = false, $action_id = null, $user_id = null)
{
    if (!$rules || empty($action_id) || empty($user_id)) {
        return false;
    }

    $return = true;
    foreach ($rules as $rule) {

        // 检查执行周期
        $map = array(
            'action_id' => $action_id,
            'user_id'   => $user_id,
        );
        $map['create_time'] = array(
            'gt',
            NOW_TIME - intval($rule['cycle']) * 3600,
        );
        $exec_count = M('ActionLog')->where($map)->count();
        if ($exec_count > $rule['max']) {
            continue;
        }

        // 执行数据库操作
        $Model = M(ucfirst($rule['table']));
        $field = $rule['field'];
        $res   = $Model->where($rule['condition'])->setField($field, array(
            'exp',
            $rule['rule'],
        ));

        if (!$res) {
            $return = false;
        }
    }
    return $return;
}

// 基于数组创建目录和文件
function create_dir_or_files($files)
{
    foreach ($files as $key => $value) {
        if (substr($value, -1) == '/') {
            mkdir($value);
        } else {
            @file_put_contents($value, '');
        }
    }
}

if (!function_exists('array_column')) {
    function array_column(array $input, $columnKey, $indexKey = null)
    {
        $result = array();
        if (null === $indexKey) {
            if (null === $columnKey) {
                $result = array_values($input);
            } else {
                foreach ($input as $row) {
                    $result[] = $row[$columnKey];
                }
            }
        } else {
            if (null === $columnKey) {
                foreach ($input as $row) {
                    $result[$row[$indexKey]] = $row;
                }
            } else {
                foreach ($input as $row) {
                    $result[$row[$indexKey]] = $row[$columnKey];
                }
            }
        }
        return $result;
    }
}

/**
 * 获取表名（不含表前缀）
 *
 * @param string $model_id
 * @return string 表名
 * @author huajie <banhuajie@163.com>
 */
function get_table_name($model_id = null)
{
    if (empty($model_id)) {
        return false;
    }
    $Model = M('model');
    $info  = $Model->getById($model_id);
    return $info['name'];
}

/**
 * 获取属性信息并缓存
 *
 * @param integer $id
 *            属性ID
 * @param string $field
 *            要获取的字段名
 * @return string 属性信息
 */
function get_model_attribute($model_id, $field_sort = false)
{
    static $list;

    /* 非法ID */
    if (empty($model_id) || !is_numeric($model_id)) {
        return '';
    }

    /* 获取属性 */
    if (!isset($list[$model_id])) {
        $map['model_id'] = $map2['id'] = $model_id;

        $alist = M('attribute')->where($map)->order('id asc')->select();
        foreach ($alist as $vo) {
            $res[$vo['name']] = $vo;
        }
        unset($alist);

        $field_sort === false && $field_sort = M('model')->where($map2)->getField('field_sort');
        if ($field_sort) {
            $field_sort = json_decode($field_sort, true);
            $sort       = array();
            foreach ($field_sort as $s) {
                $sort[$s] = $res[$s];
                unset($res[$s]);
            }

            $res = array_merge($sort, $res);
        }

        $list[$model_id] = $res;
    }

    return (array) $list[$model_id];
}

/**
 * 调用系统的API接口方法（静态方法）
 * api('User/getName','id=5'); 调用公共模块的User接口的getName方法
 * api('Admin/User/getName','id=5'); 调用Admin模块的User接口
 *
 * @param string $name
 *            格式 [模块名]/接口名/方法名
 * @param array|string $vars
 *            参数
 */
function api($name, $vars = array())
{
    $array     = explode('/', $name);
    $method    = array_pop($array);
    $classname = array_pop($array);
    $module    = $array ? array_pop($array) : 'Common';
    $callback  = $module . '\\Api\\' . $classname . 'Api::' . $method;
    if (is_string($vars)) {
        parse_str($vars, $vars);
    }
    return call_user_func_array($callback, $vars);
}

/**
 * 根据条件字段获取指定表的数据
 *
 * @param mixed $value
 *            条件，可用常量或者数组
 * @param string $condition
 *            条件字段
 * @param string $field
 *            需要返回的字段，不传则返回整个数据
 * @param string $table
 *            需要查询的表
 * @author huajie <banhuajie@163.com>
 */
function get_table_field($value = null, $condition = 'id', $field = null, $table = null)
{
    if (empty($value) || empty($table)) {
        return false;
    }

    // 拼接参数
    $map[$condition] = $value;
    $info            = M(ucfirst($table))->where($map);
    if (empty($field)) {
        $info = $info->field(true)->find();
    } else {
        $info = $info->getField($field);
    }
    return $info;
}

/**
 * 获取文档封面图片
 *
 * @param int $cover_id
 * @param string $field
 * @return 完整的数据 或者 指定的$field字段值
 * @author huajie <banhuajie@163.com>
 */
function get_cover($cover_id, $field = null)
{
    if (empty($cover_id)) {
        return false;
    }

    $key     = 'Picture_' . $cover_id;
    $picture = S($key);

    if (!$picture) {
        $map['status'] = 1;
        $picture       = M('Picture')->where($map)->getById($cover_id);
        S($key, $picture, 86400);
    }

    if (empty($picture)) {
        return '';
    }

    return empty($field) ? $picture : $picture[$field];
}

function get_cover_url($cover_id, $width = '', $height = '')
{
    if (strpos($cover_id, 'http') === 0)
        return $cover_id;
    $info = get_cover($cover_id);
    if ($width > 0 && $height > 0) {
        $thumb = "?imageMogr2/thumbnail/{$width}x{$height}";
    } elseif ($width > 0) {
        $thumb = "?imageMogr2/thumbnail/{$width}x";
    } elseif ($height > 0) {
        $thumb = "?imageMogr2/thumbnail/x{$height}";
    }
//    if ($width || $height) {
//        $path = '';
//        if ($info['url']) {
//            $path = mk_rule_image($info['url'], $width, $height);
//        } else {
//            if (empty ($info ['path']))
//                return '';
//            $path = mk_rule_image($info['path'], $width, $height);
//        }
//        return $path . $thumb;
//    } else {
    if ($info ['url'])
        return $info ['url'] . $thumb;

    $url = $info ['path'];
    if (empty ($url))
        return '';
    return SITE_URL . $url . $thumb;
//    }
}




function get_square_url($cover_id, $width = '')
{
    $info = get_cover($cover_id);

    if ($width > 0) {
        $thumb = "?imageView2/1/w/{$width}";
    }
    if ($info['url']) {
        return $info['url'] . $thumb;
    }

    $url = $info['path'];
    if (empty($url)) {
        return '';
    }

    return SITE_URL . $url . $thumb;
}
// 兼容旧方法
function get_picture_url($cover_id)
{
    return get_cover_url($cover_id);
}
function get_img_html($cover_id)
{
    $url = get_cover_url($cover_id);

    return url_img_html($url);
}

function get_img_list_html($str)
{
    if (empty($str)) {
        return;
    }

    $imageList         = str2arr($str);
    $url_img_list_html = '';
    foreach ($imageList as $cover_id) {
        $url = get_cover_url($cover_id);

        $url_img_list_html .= url_img_html($url);
    }
    return $url_img_list_html;
}

function get_img_list_url($str)
{
    if (empty($str)) {
        return;
    }
    // echo $str;

    $imageList = str2arr($str);
    foreach ($imageList as $key => $cover_id) {
        $imageList[$key] = get_cover_url($cover_id);
    }
    return $imageList;
}
function url_img_html($url)
{
    if (empty($url)) {
        return '';
    }

    return '<img class="list_img" src="' . $url . '" >';
}

function get_file_list_html($str)
{
    if (empty($str)) {
        return;
    }
    $imageList         = str2arr($str);
    $url_img_list_html = '';
    foreach ($imageList as $cover_id) {
        $url = get_file_urls($cover_id);
        $url_img_list_html .= url_file_html($url['savepath'],$url['name']);
    }
    return $url_img_list_html;
}

function get_file_urls($file_id)
{
    if (strpos($file_id, 'http') === 0)
        return $file_id;
    $info = get_file_url_rewrite($file_id);
    if ($info ['name'])
        return [
            'name' => $info ['name'],
            'savepath' => $info ['savepath'],
        ];

    $url = $info ['savepath'];
    if (empty ($url))
        return '';
    return SITE_URL . $url;
//    }
}

function get_file_url_rewrite($id){
    if (empty($id)) {
        return false;
    }
    $key  = 'File_' . $id;
    $file = S($key);
    if (!$file) {
        $file = M('File')->where(array(
            'id' => $id,
        ))->find();
        S($key, $file, 86400);
    }
    return $file;
}

function url_file_html($url,$name)
{
    if (empty($url)) {
        return '';
    }

    return '<a target="_blank" href="' . $url . '?attname=' . $name . '">' . $name . '</a> ';
}


/* 根据id获取fiel路径 */
function get_file_url($id)
{
    if (empty($id)) {
        return false;
    }

    $key  = 'File_' . $id;
    $file = S($key);

    if (!$file) {
        $file = M('File')->where(array(
            'id' => $id,
        ))->find();
        S($key, $file, 86400);
    }

    if (empty($file)) {
        return '';
    }

    $info = $file;

    if ($info['url']) {
        return $info['url'];
    }

    $url = $info['savepath'];
    if (empty($url)) {
        return '';
    }

    return SITE_URL . '/Uploads/Download/' . $info['savepath'] . $info['savename'];
}
/**
 * 检查$pos(推荐位的值)是否包含指定推荐位$contain
 *
 * @param number $pos
 *            推荐位的值
 * @param number $contain
 *            指定推荐位
 * @return boolean true 包含 ， false 不包含
 * @author huajie <banhuajie@163.com>
 */
function check_document_position($pos = 0, $contain = 0)
{
    if (empty($pos) || empty($contain)) {
        return false;
    }

    // 将两个参数进行按位与运算，不为0则表示$contain属于$pos
    $res = $pos & $contain;
    if ($res !== 0) {
        return true;
    } else {
        return false;
    }
}

/**
 * 获取数据的所有子孙数据的id值
 *
 * @author 朱亚杰 <xcoolcc@gmail.com>
 */
function get_stemma($pids, Model &$model, $field = 'id')
{
    $collection = array();

    // 非空判断
    if (empty($pids)) {
        return $collection;
    }

    if (is_array($pids)) {
        $pids = trim(implode(',', $pids), ',');
    }
    $result = $model->field($field)->where(array(
        'pid' => array(
            'IN',
            (string) $pids,
        ),
    ))->select();
    $child_ids = array_column((array) $result, 'id');

    while (!empty($child_ids)) {
        $collection = array_merge($collection, $result);
        $result     = $model->field($field)->where(array(
            'pid' => array(
                'IN',
                $child_ids,
            ),
        ))->select();
        $child_ids = array_column((array) $result, 'id');
    }
    return $collection;
}

/**
 * 判断关键词是否唯一
 *
 * @author weiphp
 */
function keyword_unique($keyword)
{
    if (empty($keyword)) {
        return false;
    }

    $map['keyword'] = $keyword;
    $info           = M('keyword')->where($map)->find();
    return empty($info);
}
// 分析枚举类型配置值 格式 a:名称1,b:名称2
// weiphp 该函数是从admin的function的文件里提取这到里
function parse_config_attr($string)
{
    $array = preg_split('/[\s;\r\n]+/', trim($string, ",;\s\r\n"));
    if (strpos($string, ':')) {
        $value = array();
        foreach ($array as $val) {
            list($k, $v) = explode(':', $val);
            $value[$k]   = $v;
        }
    } else {
        $value = $array;
    }
    foreach ($value as &$vo) {
        $vo = clean_hide_attr($vo);
    }
    // dump($value);
    return $value;
}
function parse_config_attr_app($string)
{
    $array = preg_split('/[\s;\r\n]+/', trim($string, ",;\s\r\n"));
    if (strpos($string, ':')) {
        $value = array();
        foreach ($array as $index => $val) {
            list($k, $v) = explode(':', $val);
//            $value [$k] = $v;
            $value[$index]['id']    = $k;
            $value[$index]['value'] = $v;
        }
    } else {
        $value = $array;
    }
    foreach ($value as &$vo) {
        $vo['value'] = clean_hide_attr($vo['value']);
    }
    // dump($value);
    return $value;
}
function clean_hide_attr($str)
{
    $arr = explode('|', $str);
    return $arr[0];
}
function get_hide_attr($str)
{
    $arr = explode('|', $str);
    return $arr[1];
}
/*
// 分析枚举类型字段值 格式 a:名称1,b:名称2
// 暂时和 parse_config_attr功能相同
// 但请不要互相使用，后期会调整
function parse_field_attr($string) {
if (0 === strpos ( $string, ':' )) {
// 采用函数定义
return eval ( substr ( $string, 1 ) . ';' );
}
$array = preg_split ( '/[\s;\r\n]+/', trim ( $string, ",;\r\n" ) );
// dump($array);
if (strpos ( $string, ':' )) {
$value = array ();
foreach ( $array as $val ) {
list ( $k, $v ) = explode ( ':', $val );
empty ( $v ) && $v = $k;
$k = clean_hide_attr ( $k );
$value [$k] = $v;
}
} else {
$value = $array;
}
// dump($value);
return $value;
}
 */

// 分析枚举类型字段值 格式 a:名称1,b:名称2
// 暂时和 parse_config_attr功能相同
// 但请不要互相使用，后期会调整
function parse_field_attr($string)
{
    if (0 === strpos($string, ':')) {
        // 采用函数定义

        $r = eval('return ' . substr($string, 1) . ';');

        return $r;

    }
    $array = preg_split('/[\s;\r\n]+/', trim($string, ",;\r\n"));
    // dump($array);
    if (strpos($string, ':')) {
        $value = array();
        foreach ($array as $val) {
            list($k, $v)    = explode(':', $val);
            empty($v) && $v = $k;
            $k              = clean_hide_attr($k);
            $value[$k]      = $v;
        }
    } else {
        $value = $array;
    }
    // dump($value);
    return $value;
}
/* 解析列表定义规则 */
function get_list_field($data, $grid, $model)
{
    // 获取当前字段数据
    $field      = $grid['field'];
    $array      = explode('|', $field);
    $field_name = trim($array[0]);
    $value      = $data[$field_name];

    // 函数支持
    if (isset($array[1])) {
        if ($array[1] == 'get_name_by_status') {
            $value = get_name_by_status($value, $field_name, $model['id']);
        } else {
            $value = call_user_func($array[1], $value);
        }
    }

    // 链接支持
    if (!empty($grid['href'])) {
        $links = explode(',', $grid['href']);
        foreach ($links as $link) {
            $array = explode('|', $link);
            $href  = $array[0];

            $show = isset($array[1]) ? $array[1] : $value;
            // 增加跳转方式处理 weiphp
            $target = '_self';
            if (preg_match('/target=(\w+)/', $href, $matches)) {
                $target = $matches[1];
                $href   = str_replace('&' . $matches[0], '', $href);
            }

            // 替换系统特殊字符串
            $href = str_replace(array(
                '[DELETE]',
                '[EDIT]',
                '[MODEL]',
            ), array(
                'del?id=[id]&model=[MODEL]',
                'edit?id=[id]&model=[MODEL]',
                $model['id'],
            ), $href);

            // 替换数据变量
            $href = preg_replace_callback('/\[([a-z_]+)\]/', function ($match) use ($data) {
                return $data[$match[1]];
            }, $href);

            // 兼容多种写法
            if (strpos($href, '?') === false && strpos($href, '&') !== false) {
                $href = preg_replace("/&/i", "?", $href, 1);
            }
            if ($show == '删除') {
                $val[] = '<a class="confirm"   href="' . urldecode(U($href, $GLOBALS['get_param'])) . '">' . $show . '</a>';
            } else if ($show == '复制链接') {
                $paramArrs = $GLOBALS['get_param'];
                unset($paramArrs['mdm']);
                $publicid = get_token_appinfo('', 'id');
                $val[]    = '<a class="list_copy_link" id="copyLink_' . $data['id'] . '"   data-clipboard-text="' . urldecode(U($href, $paramArrs)) . '&publicid=' . $publicid . '">' . $show . '</a>';
            } else {
                // 排除GET里的参数影响到已赋值的参数
                $url_param = array();
                foreach ($GLOBALS['get_param'] as $key => $gp) {
                    if (strpos($href, $key . '=') === false) {
                        $url_param[$key] = $gp;
                    }
                }
                $val[] = '<a  target="' . $target . '" href="' . urldecode(U($href, $url_param)) . '">' . $show . '</a>';
            }
        }
        $value = implode(' ', $val);
    }
    return $value;
}
/**
 * 获取状态值对应的标题
 *
 * @author weiphp
 */
function get_name_by_status($val, $name, $model_id)
{
    static $_name = array();
    if (!isset($_name[$model_id])) {
        $_name[$model_id] = array();
        $map['extra']     = array(
            'EXP',
            '!=""',
        );
        $map['model_id'] = $model_id;
        $list            = M('attribute')->where($map)->select();
        foreach ($list as $attr) {
            if (empty($attr['extra'])) {
                continue;
            }

            $extra = parse_config_attr($attr['extra']);
            if (is_array($extra) && !empty($extra)) {
                $_name[$model_id][$attr['name']]['value'] = $extra;
                $_name[$model_id][$attr['name']]['type']  = $attr['type'];
            }
        }
    }

    if ($_name[$model_id][$name]['type'] == 'checkbox') {
        $valArr = explode(',', $val);
        foreach ($valArr as $v) {
            $res[] = empty($_name[$model_id][$name]['value'][$v]) ? $v : $_name[$model_id][$name]['value'][$v];
        }

        return implode(', ', $res);
    } else {
        return empty($_name[$model_id][$name]['value'][$val]) ? $val : $_name[$model_id][$name]['value'][$val];
    }
}
function addWeixinLog($data, $data_post = '', $wechat = false)
{
    $log['cTime']        = time();
    $log['cTime_format'] = date('Y-m-d H:i:s', $log['cTime']);
    $log['data']         = is_array($data) ? serialize($data) : $data;
    $log['data_post']    = is_array($data_post) ? serialize($data_post) : $data_post;
    M('weixin_log')->add($log);

    // 通过微信客服接口直接回复调试信息
    if ($wechat && C('SEND_CUSTOM_MSG')) {
        $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . get_access_token();

        $param['touser']          = get_openid();
        $param['msgtype']         = 'text';
        $param['text']['content'] = 'data: ' . $log['data'] . '<br/> data_post: ' . $log['data_post'];

        post_data($url, $param);
    }
}
/**
 * 取一个二维数组中的每个数组的固定的键知道的值来形成一个新的一维数组
 *
 * @param $pArray 一个二维数组
 * @param $pKey 数组的键的名称
 * @return 返回新的一维数组
 */
function getSubByKey($pArray, $pKey = "", $pCondition = "")
{
    $result = array();
    if (is_array($pArray)) {
        foreach ($pArray as $temp_array) {
            if (is_object($temp_array)) {
                $temp_array = (array) $temp_array;
            }
            if (("" != $pCondition && $temp_array[$pCondition[0]] == $pCondition[1]) || "" == $pCondition) {
                $result[] = ("" == $pKey) ? $temp_array : isset($temp_array[$pKey]) ? $temp_array[$pKey] : "";
            }
        }
        return $result;
    } else {
        return false;
    }
}
// 判断是否是在微信浏览器里
function isWeixinBrowser($from = 0)
{
    if ((!$from && defined('IN_WEIXIN') && IN_WEIXIN) || isset($_GET['is_stree'])) {
        return true;
    }

    $agent = $_SERVER['HTTP_USER_AGENT'];
    if (!strpos($agent, "icroMessenger")) {
        return false;
    }
    return true;
}
// php获取当前访问的完整url地址
function GetCurUrl()
{
    $url = HTTP_PREFIX;
    if ($_SERVER['SERVER_PORT'] != '80' && $_SERVER['SERVER_PORT'] != '443') {
        $url .= $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
    } else {
        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    }
    // 兼容后面的参数组装
    if (stripos($url, '?') === false) {
        $url .= '?t=' . time();
    }
    return $url;
}
// 获取当前用户的OpenId
function get_openid($openid = null)
{
    $token = get_token();
    if ($openid !== null && $openid != '-1' && $openid != '-2') {
        session('openid_' . $token, $openid);
    } elseif (!empty($_REQUEST['openid']) && $_REQUEST['openid'] != '-1' && $_REQUEST['openid'] != '-2') {
        session('openid_' . $token, $_REQUEST['openid']);
    }
    $openid = session('openid_' . $token);

    $isWeixinBrowser = isWeixinBrowser();
    if ((empty($openid) || $openid == '-1') && $isWeixinBrowser && $_REQUEST['openid'] != '-2' && IS_GET && !IS_AJAX) {
        $callback = GetCurUrl();
        $openid   = OAuthWeixin($callback, $token, true);
        if ($openid != false && $openid != '-2') {
            session('openid_' . $token, $openid);
        }
    }
    if (empty($openid)) {
        return '-1';
    }
    return $openid;
}
// 通过UID获取openid
function getOpenidByUid($uid, $token = '')
{
    empty($token) && $token = get_token();

    $map['uid']   = $uid;
    $map['token'] = $token;
    return M('public_follow')->where($map)->getField('openid');
}
/*
 * 获取支付的appid的openid
 * 微信支付和红包使用
 */
function getPaymentOpenid($appId = "", $serect = "")
{
    if (empty($appId) || empty($serect)) {

        $openid = get_openid();
        return $openid;
        exit();
    }
    $callback = GetCurUrl();

    $param  = $appId . ':' . $serect;
    $openid = OAuthWeixin($callback, $param, true);

    return $openid;
}

// 获取当前用户的Token
function get_token($token = null, $publicid = null)
{
    $stoken = session('token');
    $reset  = false;
    if ($token !== null && $token != '-1') {
        session('token', $token);
        $reset = true;
    } elseif (!empty($_REQUEST['token']) && $_REQUEST['token'] != '-1') {
        session('token', $_REQUEST['token']);
        $reset = true;
    } elseif (!empty($_REQUEST['publicid']) || $publicid != null) {
        $publicid = $publicid ? $publicid : I('publicid');
        $token    = D('Common/Public')->getInfo($publicid, 'token');
        $token && session('token', $token);
        $reset = true;
    }
    $token = session('token');
    if (!empty($token) && $token != '-1' && $stoken != $token && $GLOBALS['is_wap']) {
        session('mid', null);
    }
    /*
    //加校验，防止使用无权限的公众号
    if(!$GLOBALS['is_wap'] && $reset){
    if(empty($GLOBALS['myinfo'])) $token = -1;
    else{
    $sql = 'SELECT public_id FROM `wp_public_link` as l LEFT JOIN wp_public as p on l.mp_id=p.id WHERE l.uid='.$GLOBALS['mid'];
    $list = M()->query($sql);
    $flat = false;
    foreach ($list as $value) {
    if($value['public_id']==$token){
    $flat = true;
    }
    }
    if(!$flat) $token = -1;
    }
    }
     */

    if (empty($token)) {
        $token = -1;
    }

    return $token;
}
// 获取当前用户的UID,方便在模型里的自动填充功能使用
function get_mid()
{
    return session('mid');
}
// 通过openid获取微信用户基本信息,此功能只有认证的服务号才能用
function getWeixinUserInfo($openid, $access_token = '')
{
    addWeixinLog($openid, C('USER_BASE_INFO'));
    // if (!C('USER_BASE_INFO')) {
    //     return array();
    // }
    // addWeixinLog($openid, 'init');
    $orginal                              = $access_token;
    empty($access_token) && $access_token = get_access_token();
    if (empty($access_token)) {
        return array();
    }

    $param2['access_token'] = $access_token;
    $param2['openid']       = $openid;
    $param2['lang']         = 'zh_CN';
    if (empty($orginal)) {
        $url = 'https://api.weixin.qq.com/cgi-bin/user/info?' . http_build_query($param2);
    } else {
        $url = 'https://api.weixin.qq.com/sns/userinfo?' . http_build_query($param2);
    }

    $content = get_data($url);
    $content = json_decode($content, true);
    return $content;
}
// 获取公众号的信息
function get_token_appinfo($token = '', $field = '')
{
    empty($token) && $token = get_token();
    if ($token != 'gh_3c884a361561') {
        $info = D('Common/Public')->getInfoByToken($token, $field);
    }
    return $info;
}
// 兼容旧方法
function get_service_info($field = '')
{
    return get_token_appinfo('', $field);
}
// 判断公众号的类型：是订阅号还是服务号
function get_token_type($token = '')
{
    $info = get_token_appinfo($token);
    return intval($info['type']);
}
// 获取access_token，自动带缓存功能
function get_access_token($token = '', $update = false)
{
    empty($token) && $token = get_token();

    $info = get_token_appinfo($token);
    // 微信开放平台一键绑定
    if ($token == 'gh_3c884a361561' || $info['is_bind']) {
        $access_token = get_authorizer_access_token($info['appid'], $info['authorizer_refresh_token'], $update);
    } else {
        $access_token = get_access_token_by_apppid($info['appid'], $info['secret'], $update);
    }

    // 自动判断access_token是否已失效，如失效自动获取新的
    if ($update == false) {
        $url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=' . $access_token;
        $res = wp_file_get_contents($url);
        $res = json_decode($res, true);
        if ($res['errcode'] == '40001') {
            $access_token = get_access_token($token, true);
        }
    }

    return $access_token;
}
function get_authorizer_access_token($appid, $refresh_token, $update)
{
    if (empty($appid)) {
        return 0;
    }

    $key = 'authorizer_access_token_' . $appid;
    $res = S($key);
    if ($res !== false && !$update) {
        return $res;
    }

    $dao = D('Addons://PublicBind/PublicBind');
    if (empty($refresh_token)) {
        $auth_code               = $dao->_get_pre_auth_code();
        $info                    = $dao->getAuthInfo($auth_code);
        $authorizer_access_token = $info['authorization_info']['authorizer_access_token'];
    } else {
        $info                    = $dao->refreshToken($appid, $refresh_token);
        $authorizer_access_token = $info['authorizer_access_token'];
    }

    if (!empty($authorizer_access_token)) {
        S($key, $authorizer_access_token, $info['expires_in'] - 200);
        return $authorizer_access_token;
    } else {
        addWeixinLog($info, 'get_authorizer_access_token_fail_' . $appid);
        return 0;
    }
}
function get_access_token_by_apppid($appid, $secret, $update = false)
{
    if (empty($appid) || empty($secret)) {
        return 0;
    }

    $key = 'access_token_apppid_' . $appid . '_' . $secret;
    $res = S($key);
    if ($res !== false && !$update) {
        return $res;
    }

    $url     = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&secret=' . $secret . '&appid=' . $appid;
    $tempArr = json_decode(get_data($url), true);
    if (@array_key_exists('access_token', $tempArr)) {
        S($key, $tempArr['access_token'], $tempArr['expires_in']);
        return $tempArr['access_token'];
    } else {
        return 0;
    }
}
function OAuthWeixin($callback, $token = '', $is_return = false)
{
    if ((defined('IN_WEIXIN') && IN_WEIXIN) || isset($_GET['is_stree'])) {
        return false;
    }

    $isWeixinBrowser = isWeixinBrowser();
    if (!$isWeixinBrowser) {
        return false;
    }
    $callback = urldecode($callback);
    if (strpos($callback, '?') === false) {
        $callback .= '?';
    } else {
        $callback .= '&';
    }
    if (!empty($token) && strpos($token, ':') !== false) {
        $arr            = explode(':', $token);
        $info['appid']  = $arr[0];
        $info['secret'] = $arr[1];
    } else {
        if (!C('USER_OAUTH')) {
            return false;
        }

        $info = get_token_appinfo($token);
    }
    if (empty($info['appid'])) {
        redirect($callback . 'openid=-2');
    }
    $param['appid'] = $info['appid'];
    if ($_GET['state'] != 'weiphp') {
        $param['redirect_uri']                        = $callback;
        $param['response_type']                       = 'code';
        $param['scope']                               = 'snsapi_base';
        $param['state']                               = 'weiphp';
        $info['is_bind'] && $param['component_appid'] = C('COMPONENT_APPID');
        $url                                          = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($param) . '#wechat_redirect';
        redirect($url);
    } elseif ($_GET['state'] == 'weiphp') {
        if (empty($_GET['code'])) {
            exit('code获取失败');
        }

        $param['code']       = I('code');
        $param['grant_type'] = 'authorization_code';

        if ($info['is_bind']) {
            $param['appid']                  = I('appid');
            $param['component_appid']        = C('COMPONENT_APPID');
            $param['component_access_token'] = D('Addons://PublicBind/PublicBind')->_get_component_access_token();

            $url = 'https://api.weixin.qq.com/sns/oauth2/component/access_token?' . http_build_query($param);
        } else {
            $param['secret'] = $info['secret'];

            $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?' . http_build_query($param);
        }

        $content = get_data($url);
        $content = json_decode($content, true);
        if ($is_return) {
            return $content['openid'];
        } else {
            redirect($callback . 'openid=' . $content['openid']);
        }
    }
}
/**
 * 执行SQL文件
 */
function execute_sql_file($sql_path)
{
    // 读取SQL文件
    $sql = wp_file_get_contents($sql_path);
    $sql = str_replace("\r", "\n", $sql);
    $sql = explode(";\n", $sql);

    // 替换表前缀
    $orginal = 'wp_';
    $prefix  = C('DB_PREFIX');
    $sql     = str_replace("{$orginal}", "{$prefix}", $sql);

    // 开始安装
    foreach ($sql as $value) {
        $value = trim($value);
        if (empty($value)) {
            continue;
        }

        $res = M()->execute($value);
        // dump($res);
        // dump(M()->getLastSql());
    }
}
// 设置微信关联聊天中用到的用户状态
function set_user_status($addon, $keywordArr = array())
{
    // 设置用户状态
    $user_status['addon']      = $addon;
    $user_status['keywordArr'] = $keywordArr;

    $openid = get_openid();
    return S('user_status_' . $openid, $user_status);
}

// 获取公众号等级名
function get_public_group_name($group_id)
{
    static $_public_group_name;

    $group_id = intval($group_id);
    if (!isset($_public_group_name[$group_id])) {
        $group_list = M('public_group')->field('id, title')->select();
        foreach ($group_list as $g) {
            $_public_group_name[$g['id']] = $g['title'];
        }
        $_public_group_name[0] = '无';
    }

    return $_public_group_name[$group_id];
}

// 截取内容
function getShort($str, $length = 40, $ext = '')
{
    $str      = filter_line_tab($str);
    $str      = htmlspecialchars($str);
    $str      = strip_tags($str);
    $str      = htmlspecialchars_decode($str);
    $strlenth = 0;
    $out      = '';
    preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/", $str, $match);
    foreach ($match[0] as $v) {
        preg_match("/[\xe0-\xef][\x80-\xbf]{2}/", $v, $matchs);
        if (!empty($matchs[0])) {
            $strlenth += 1;
        } elseif (is_numeric($v)) {
            $strlenth += 0.5; // 字符字节长度比例 汉字为1
        } else {
            $strlenth += 0.5; // 字符字节长度比例 汉字为1
        }

        if ($strlenth > $length) {
            $output .= $ext;
            break;
        }

        $output .= $v;
    }
    return $output;
}
// 过滤非法html标签 去掉换行符
function filter_line_tab($text)
{
    $text = str_replace(array(
        "\r\n",
        "\r",
        "\n",
        " ",
    ), '', $text);
    // 过滤标签
    $text = nl2br($text);
    $text = real_strip_tags($text);
    $text = addslashes($text);
    $text = trim($text);
    return addslashes($text);
}
function real_strip_tags($str, $allowable_tags = "")
{
    $str = stripslashes(htmlspecialchars_decode($str));
    return strip_tags($str, $allowable_tags);
}

// 防超时的file_get_contents改造函数
function wp_file_get_contents($url)
{
    $context = stream_context_create(array(
        'http' => array(
            'timeout' => 30,
        ),
    )); // 超时时间，单位为秒

    return file_get_contents($url, 0, $context);
}

// 全局的安全过滤函数
function safe($text, $type = 'html')
{
    // 无标签格式
    $text_tags = '';
    // 只保留链接
    $link_tags = '<a>';
    // 只保留图片
    $image_tags = '<img>';
    // 只存在字体样式
    $font_tags = '<i><b><u><s><em><strong><font><big><small><sup><sub><bdo><h1><h2><h3><h4><h5><h6>';
    // 标题摘要基本格式
    $base_tags = $font_tags . '<p><br><hr><a><img><map><area><pre><code><q><blockquote><acronym><cite><ins><del><center><strike><section><header><footer><article><nav><audio><video>';
    // 兼容Form格式
    $form_tags = $base_tags . '<form><input><textarea><button><select><optgroup><option><label><fieldset><legend>';
    // 内容等允许HTML的格式
    $html_tags = $base_tags . '<meta><ul><ol><li><dl><dd><dt><table><caption><td><th><tr><thead><tbody><tfoot><col><colgroup><div><span><object><embed><param>';
    // 全HTML格式
    $all_tags = $form_tags . $html_tags . '<!DOCTYPE><html><head><title><body><base><basefont><script><noscript><applet><object><param><style><frame><frameset><noframes><iframe>';
    // 过滤标签
    $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
    $text = strip_tags($text, ${$type . '_tags'});

    // 过滤攻击代码
    if ($type != 'all') {
        // 过滤危险的属性，如：过滤on事件lang js
        while (preg_match('/(<[^><]+)(ondblclick|onclick|onload|onerror|unload|onmouseover|onmouseup|onmouseout|onmousedown|onkeydown|onkeypress|onkeyup|onblur|onchange|onfocus|codebase|dynsrc|lowsrc)([^><]*)/i', $text, $mat)) {
            $text = str_ireplace($mat[0], $mat[1] . $mat[3], $text);
        }
        while (preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i', $text, $mat)) {
            $text = str_ireplace($mat[0], $mat[1] . $mat[3], $text);
        }
    }
    return $text;
}
// 创建多级目录
function mkdirs($dir)
{
    if (!is_dir($dir)) {
        if (!mkdirs(dirname($dir))) {
            return false;
        }
        if (!mkdir($dir, 0777)) {
            return false;
        }
    }
    return true;
}
// 组装查询条件
function getIdsForMap($ids, $map = array(), $field = 'id')
{
    $ids = safe($ids);
    $ids = preg_split('/[\s,;]+/', $ids); // 支持以空格tab逗号分号分割ID
    $ids = array_filter($ids);
    if (empty($ids)) {
        return $map;
    }

    $map[$field] = array(
        'in',
        $ids,
    );

    return $map;
}
// 获取通用分类级联菜单的标题，改方法仅支持级联的数据源配置成数据表common_category的情况，其它情况需要使用下面的getCascadeTitle方法
function getCommonCategoryTitle($ids)
{
    $extra = 'type=db&table=common_category';

    return getCascadeTitle($ids, $extra);
}
// 获取级联菜单的标题的通用处理方法
function getCascadeTitle($ids, $extra)
{
    $idArr = explode(',', $ids);
    $idArr = array_filter($idArr);
    if (empty($idArr)) {
        return '';
    }

    parse_str($extra, $arr);
    if ($arr['type'] == 'db') {
        $table = $arr['table'];
        unset($arr['type'], $arr['table']);

        $arr['token'] = get_token();
        $arr['id']    = array(
            'in',
            $idArr,
        );
        $list     = M($table)->where($arr)->field('title')->select();
        $titleArr = getSubByKey($list, 'title');
    } else {
        $str = str_replace('，', ',', $extra);
        $str = str_replace('【', '[', $str);
        $str = str_replace('】', ']', $str);
        $str = str_replace('：', ':', $str);

        $arr = StringToArray($str);
        $str = '';
        foreach ($arr as $v) {
            if ($v == '[' || $v == ']' || $v == ',') {
                if ($str) {
                    $block = explode(':', trim($str));
                    if (in_array($block[0], $idArr)) {
                        $titleArr[] = isset($block[1]) ? $block[1] : $block[0];
                    }
                }
                $str = '';
            } else {
                $str .= $v;
            }
        }
    }
    return implode(' > ', $titleArr);
}
// 把字符串转成数组，支持汉字，只能是utf-8格式的
function StringToArray($str)
{
    $result = array();
    $len    = strlen($str);
    $i      = 0;
    while ($i < $len) {
        $chr = ord($str[$i]);
        if ($chr == 9 || $chr == 10 || (32 <= $chr && $chr <= 126)) {
            $result[] = substr($str, $i, 1);
            $i += 1;
        } elseif (192 <= $chr && $chr <= 223) {
            $result[] = substr($str, $i, 2);
            $i += 2;
        } elseif (224 <= $chr && $chr <= 239) {
            $result[] = substr($str, $i, 3);
            $i += 3;
        } elseif (240 <= $chr && $chr <= 247) {
            $result[] = substr($str, $i, 4);
            $i += 4;
        } elseif (248 <= $chr && $chr <= 251) {
            $result[] = substr($str, $i, 5);
            $i += 5;
        } elseif (252 <= $chr && $chr <= 253) {
            $result[] = substr($str, $i, 6);
            $i += 6;
        }
    }
    return $result;
}
/**
 * 增加用户积分函数
 *
 * @param string $name
 *            积分标识名
 * @param int $lock_time
 *            解锁时间，即多长时间内才能重复加积分，为0时不作控制
 * @param array $credit
 *            自定义金币值，格式：array('score'=>金币值,'experience'=>经历值,'title'=>'积分项名称');为空时默认取管理中心积分管理里的配置值
 * @param int $admin_uid
 *            管理员UID，用于管理员给用户手工加积分时的场景
 */
function add_credit($name, $lock_time = 5, $credit = array(), $admin_uid = 0)
{
    if (empty($name)) {
        return false;
    }

    if ($lock_time > 0) {
        $key = 'credit_lock_' . get_token() . '_' . get_openid() . '_' . $name;
        if (S($key)) {
            return false;
        }

        S($key, 1, $lock_time);
    }

    $data['credit_name'] = $name;
    $data['admin_uid']   = $admin_uid;
    $data                = array_merge($data, $credit);

    $credit = D('Common/Credit')->addCredit($data);

    return $credit;
}
/**
 * 增加用户余额函数
 *
 * @param int $uid
 *            用户UID
 * @param float $money
 *            充值金额
 * @param string $log
 *            recharge_log 表需要的数据
 */
function add_money($uid, $money, $log = array())
{
    if (empty($uid) || empty($money)) {
        return false;
    }

    return D('Addons://Card/Card')->addMoney($uid, $money, $log);
}
// 判断用户最大可创建的公众号数
function getPublicMax($uid)
{
    $map['uid']   = $uid;
    $public_count = M('user')->where($map)->getField('public_count');
    if ($public_count === null) {
        $public_count = C('DEFAULT_PUBLIC_CREATE_MAX_NUMB');
    }
    return intval($public_count);
}


function getPublicList()
{
    $map['b.uid'] = get_mid();
    $all          = M('public a')->join('wp_public_link b on a.id=b.mp_id')
        ->where($map)->field('a.*')->select();
    $arr = array(0 => '请选择');
    foreach ($all as $value) {
        $arr[$value['id']] = $value['public_name'];
    }
    return $arr;
}


function diyPage($keyword)
{
    $map['keyword'] = $keyword;
    $map['token']   = get_token();
    $page           = M('diy')->where($map)->find();

    if (!$page) {
        $map['token'] = '0';
        $page         = M('diy')->where($map)->find();
    }
    // dump($page);
    if (!$page) {
        return false;
    }

    $model = A('Addons://Diy/Diy');
    // dump($model);exit;
    $model->show($page['id']);
}
// 各插件获取关联抽奖活动的地址 暂只支持刮刮卡
function event_url($addon_title, $id = '0')
{
    // $map ['token'] = get_token ();
    // $map ['addon_condition'] = array (
    // 'exp',
    // "='[{$addon_title}:*]' or addon_condition='[{$addon_title}:{$id}]'"
    // );

    // $event = M ( 'Scratch' )->where ( $map )->order ( 'id desc' )->find ();
    $event_url = '';
    // if ($event) {
    // $param ['token'] = get_token ();
    // $param ['openid'] = get_openid ();
    // $param ['id'] = $event ['id'];
    // $event_url = addons_url ( 'Scratch://Scratch/show', $param );
    // }
    return $event_url;
}
// 抽奖或者优惠券领取的插件条件判断
function addon_condition_check($addon_condition)
{
    if (empty($addon_condition)) {
        return true;
    }

    preg_match_all("/\[([\s\S]*):([\*,\d]*)\]/i", $addon_condition, $match);
    if (empty($match[1][0]) || empty($match[2][0])) {
        return true;
    }

    $conditon['token'] = get_token();
    $conditon['uid']   = get_mid();
    switch ($match[1][0]) {
        case '投票':
            $match[2][0] != '*' && $match[2][0] > 0 && $conditon['vote_id'] = $match[2][0];
            $conditon['user_id']                                            = get_mid();
            unset($conditon['uid']);
            $res = M('vote_log')->where($conditon)->find();
            break;
        case '通用表单':
            $match[2][0] != '*' && $match[2][0] > 0 && $conditon['forms_id'] = $match[2][0];
            $res                                                             = M('forms_value')->where($conditon)->find();
            break;
        case '微考试':
            $match[2][0] != '*' && $match[2][0] > 0 && $conditon['exam_id'] = $match[2][0];
            $res                                                            = M('exam_answer')->where($conditon)->find();
            break;
        case '微测试':
            $match[2][0] != '*' && $match[2][0] > 0 && $conditon['test_id'] = $match[2][0];
            $res                                                            = M('test_answer')->where($conditon)->find();
            break;
        case '微调研':
            $match[2][0] != '*' && $match[2][0] > 0 && $conditon['survey_id'] = $match[2][0];
            $res                                                              = M('survey_answer')->where($conditon)->find();
            break;
        default:
            $match[2][0] != '*' && $match[2][0] > 0 && $conditon['id'] = $match[2][0];
            $res                                                       = M($match[1][0])->where($conditon)->find();
    }
    // dump ( $res );
    // dump ( M ()->getLastSql () );

    return !empty($res);
}
// 抽奖或者优惠券领取的插件条件提示
function condition_tips($addon_condition)
{
    if (empty($addon_condition)) {
        return '';
    }

    preg_match_all("/\[([\s\S]*):([\*,\d]*)\]/i", $addon_condition, $match);
    if (empty($match[1][0]) || empty($match[2][0])) {
        return '';
    }

    $conditon['token'] = get_token();
    $conditon['id']    = $match[2][0];
    $title             = '';
    $has_title         = $conditon['id'] != '*' && $conditon['id'] > 0;

    switch ($match[1][0]) {
        case '投票':
            $has_title && $title = M('vote')->where($conditon)->getField('title');
            break;
        case '通用表单':
            $has_title && $title = M('forms')->where($conditon)->getField('title');
            break;
        case '微考试':
            $has_title && $title = M('exam')->where($conditon)->getField('title');
            break;
        case '微测试':
            $has_title && $title = M('test')->where($conditon)->getField('title');
            break;
        case '微调研':
            $has_title && $title = M('survey')->where($conditon)->getField('title');
            break;
        default:
            $has_title && $title = M($match[1][0])->where($conditon)->getField('title');
    }
    $result = '需要参与' . $title . $match[1][0] . '后才能领取';

    return $result;
}
function lastsql()
{
    dump(M()->getLastSql());
}
// 商业代码解密
function code_decode($text)
{
    $key = substr(C('WEIPHP_STORE_LICENSE'), 0, 5);
    return think_decrypt($text, $key);
}
function outExcel($dataArr, $fileName = '', $sheet = false)
{
    require_once VENDOR_PATH . 'download-xlsx.php';
    export_csv($dataArr, $fileName, $sheet);
    unset($sheet);
    unset($dataArr);
}
// 获取通用分类表的分类标题
function category_title($cate_id)
{
    static $_category_title = array();
    if (isset($_category_title[$cate_id])) {
        return $_category_title[$cate_id];
    }

    $map['token'] = get_token();
    $list         = M('common_category')->where($map)->field('id,title')->select();
    foreach ($list as $v) {
        $_category_title[$v['id']] = $v['title'];
    }
    if (!isset($_category_title[$cate_id])) {
        $_category_title[$cate_id] = '';
    }
    return $_category_title[$cate_id];
}
function get_lecturer_name($lecturer_id)
{
    static $_lecturer_name = array();
    if (isset($_lecturer_name[$lecturer_id])) {
        return $_lecturer_name[$lecturer_id];
    }

    $map['token'] = get_token();
    $list         = M('classes_lecturer')->where($map)->field('id,name')->select();
    foreach ($list as $v) {
        $_lecturer_name[$v['id']] = $v['name'];
    }
    if (!isset($_lecturer_name[$lecturer_id])) {
        $_lecturer_name[$lecturer_id] = '';
    }
    return $_lecturer_name[$lecturer_id];
}
function check_token_purview($table, $id, $field = 'token')
{
    $token     = get_token();
    $map['id'] = $id;
    $info      = M($table)->where($map)->field($field)->find();
    if ($info === false || $info[$field] == $token) {
        return true;
    }
    // 没有这个字段或者没有这个记录直接返回

    exit('非法访问');
}
// weiphp专用分割函数，同时支持常见的按空格、逗号、分号、换行进行分割
function wp_explode($string, $delimiter = "\s,;\r\n")
{
    if (empty($string)) {
        return array();
    }

    // 转换中文符号
    // $string = iconv ( 'utf-8', 'gbk', $string );
    // $string = preg_replace ( '/\xa3([\xa1-\xfe])/e', 'chr(ord(\1)-0x80)', $string );
    // $string = iconv ( 'gbk', 'utf-8', $string );

    $arr = preg_split('/[' . $delimiter . ']+/', $string);
    return array_unique(array_filter($arr));
}
function get_code_img($qr_code)
{
    if (!$qr_code) {
        return '';
    }

    $html = '<img src="' . $qr_code . '" width="50" height="50" />';
    return $html;
}
function get_file_title($attach_ids)
{
    $ids = wp_explode($attach_ids);
    if (empty($ids)) {
        return '';
    }

    $map['id'] = array(
        'in',
        $ids,
    );
    $names = M('file')->where($map)->getFields('name');

    return implode(', ', $names);
}
// 阿拉伯数字转中文表述，如101转成一百零一
function num2cn($number)
{
    $number = intval($number);
    $capnum = array(
        "零",
        "一",
        "二",
        "三",
        "四",
        "五",
        "六",
        "七",
        "八",
        "九",
    );
    $capdigit = array(
        "",
        "十",
        "百",
        "千",
        "万",
    );

    $data_arr = str_split($number);
    $count    = count($data_arr);
    for ($i = 0; $i < $count; $i++) {
        $d     = $capnum[$data_arr[$i]];
        $arr[] = $d != '零' ? $d . $capdigit[$count - $i - 1] : $d;
    }
    $cncap = implode("", $arr);

    $cncap                       = preg_replace("/(零)+/", "0", $cncap); // 合并连续“零”
    $cncap                       = trim($cncap, '0');
    $cncap                       = str_replace("0", "零", $cncap); // 合并连续“零”
    $cncap == '一十' && $cncap = '十';
    $cncap == '' && $cncap       = '零';
    // echo ( $data.' : '.$cncap.' <br/>' );
    return $cncap;
}
function week_name($number = null)
{
    if ($number === null) {
        $number = date('w');
    }

    $arr = array(
        "日",
        "一",
        "二",
        "三",
        "四",
        "五",
        "六",
    );

    return '星期' . $arr[$number];
}
// 日期转换成星期几
function daytoweek($day = null)
{
    $day === null && $day = date('Y-m-d');
    if (empty($day)) {
        return '';
    }

    $number = date('w', strtotime($day));

    return week_name($number);
}
/**
 * select返回的数组进行整数映射转换
 *
 * @param array $map
 *            映射关系二维数组 array(
 *            '字段名1'=>array(映射关系数组),
 *            '字段名2'=>array(映射关系数组),
 *            ......
 *            )
 * @author 朱亚杰 <zhuyajie@topthink.net>
 * @return array array(
 *         array('id'=>1,'title'=>'标题','status'=>'1','status_text'=>'正常')
 *         ....
 *         )
 *
 */
function int_to_string(&$data, $map = array('status' => array(1 => '正常', -1 => '删除', 0 => '禁用', 2 => '未审核', 3 => '草稿')))
{
    if ($data === false || $data === null) {
        return $data;
    }
    $data = (array) $data;
    foreach ($data as $key => $row) {
        foreach ($map as $col => $pair) {
            if (isset($row[$col]) && isset($pair[$row[$col]])) {
                $data[$key][$col . '_text'] = $pair[$row[$col]];
            }
        }
    }
    return $data;
}
function importFormExcel($attach_id, $column, $dateColumn = array())
{
    $attach_id = intval($attach_id);
    $res       = array(
        'status' => 0,
        'data'   => '',
    );
    if (empty($attach_id) || !is_numeric($attach_id)) {
        $res['data'] = '上传文件ID无效！';
        return $res;
    }
    $file     = M('file')->where('id=' . $attach_id)->find();
    $root     = C('DOWNLOAD_UPLOAD.rootPath');
    $filename = SITE_PATH . '/Uploads/Download/' . $file['savepath'] . $file['savename'];
    // dump($filename);
    if (!file_exists($filename)) {
        $res['data'] = '上传的文件失败';
        return $res;
    }
    $extend = $file['ext'];
    if (!($extend == 'xls' || $extend == 'xlsx' || $extend == 'csv')) {
        $res['data'] = '文件格式不对，请上传xls,xlsx格式的文件';
        return $res;
    }

    vendor('PHPExcel');
    vendor('PHPExcel.PHPExcel_IOFactory');
    vendor('PHPExcel.Reader.Excel5');

    switch (strtolower($extend)) {
        case 'csv':
            $format    = 'CSV';
            $objReader = \PHPExcel_IOFactory::createReader($format)->setDelimiter(',')->setInputEncoding('GBK')->setEnclosure('"')->setLineEnding("\r\n")->setSheetIndex(0);
            break;
        case 'xls':
            $format    = 'Excel5';
            $objReader = \PHPExcel_IOFactory::createReader($format);
            break;
        default:
            $format    = 'excel2007';
            $objReader = \PHPExcel_IOFactory::createReader($format);
    }

    $objPHPExcel = $objReader->load($filename);
    $objPHPExcel->setActiveSheetIndex(0);
    $sheet      = $objPHPExcel->getSheet(0);
    $highestRow = $sheet->getHighestRow(); // 取得总行数
    for ($j = 2; $j <= $highestRow; $j++) {
        $addData = array();
        foreach ($column as $k => $v) {
            if ($dateColumn) {
                foreach ($dateColumn as $d) {
                    if ($k == $d) {
                        $addData[$v] = gmdate("Y-m-d H:i:s", PHPExcel_Shared_Date::ExcelToPHP($objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue()));
                    } else {
                        $addData[$v] = trim((string) $objPHPExcel->getActiveSheet()->getCell($k . $j)->getValue());
                    }
                }
            } else {
                $addData[$v] = trim((string) $objPHPExcel->getActiveSheet()->getCell($k . $j)->getValue());
            }
        }

        $isempty = true;
        foreach ($column as $v) {
            $isempty && $isempty = empty($addData[$v]);
        }

        if (!$isempty) {
            $result[$j] = $addData;
        }

    }
    $res['status'] = 1;
    $res['data']   = $result;
    return $res;
}
function showNewIcon($time, $day = 3)
{
    $img = '';
    if (NOW_TIME < ($time + 86400 * $day)) {
        $img = '<img src="' . C('TMPL_PARSE_STRING.__IMG__') . '/new.png"/>';
    }
    return $img;
}
function replace_url($content)
{
    $param['token']  = get_token();
    $param['openid'] = get_openid();

    $sreach = array(
        '[follow]',
        '[website]',
        '[token]',
        '[openid]',
    );
    $replace = array(
        addons_url('UserCenter://UserCenter/bind', $param),
        addons_url('WeiSite://WeiSite/index', $param),
        $param['token'],
        $param['openid'],
    );
    $content = str_replace($sreach, $replace, $content);

    return $content;
}
/**
 * 验证分类是否允许发布内容
 *
 * @param integer $id
 *            分类ID
 * @return boolean true-允许发布内容，false-不允许发布内容
 */
function check_category($id)
{
    if (is_array($id)) {
        $id['type'] = !empty($id['type']) ? $id['type'] : 2;
        $type       = get_category($id['category_id'], 'type');
        $type       = explode(",", $type);
        return in_array($id['type'], $type);
    } else {
        $publish = get_category($id, 'allow_publish');
        return $publish ? true : false;
    }
}

/**
 * 检测分类是否绑定了指定模型
 *
 * @param array $info
 *            模型ID和分类ID数组
 * @return boolean true-绑定了模型，false-未绑定模型
 */
function check_category_model($info)
{
    $cate  = get_category($info['category_id']);
    $array = explode(',', $info['pid'] ? $cate['model_sub'] : $cate['model']);
    return in_array($info['model_id'], $array);
}
// 获取随机的字符串，用于token，EncodingAESKey等的生成
function get_rand_char($length = 6)
{
    $str       = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    $strLength = 61;

    for ($i = 0; $i < $length; $i++) {
        $res .= $str[rand(0, $strLength)];
    }

    return $res;
}
/**
 * 根据两点间的经纬度计算距离
 *
 * @param float $lat
 *            纬度值
 * @param float $lng
 *            经度值
 */
function getDistance($lat1, $lng1, $lat2, $lng2)
{
    $earthRadius = 6367000; // approximate radius of earth in meters

    // Convert these degrees to radians to work with the formula
    $lat1 = ($lat1 * pi()) / 180;
    $lng1 = ($lng1 * pi()) / 180;

    $lat2 = ($lat2 * pi()) / 180;
    $lng2 = ($lng2 * pi()) / 180;

    // Using the Haversine formula http://en.wikipedia.org/wiki/Haversine_formula calculate the distance

    $calcLongitude      = $lng2 - $lng1;
    $calcLatitude       = $lat2 - $lat1;
    $stepOne            = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
    $stepTwo            = 2 * asin(min(1, sqrt($stepOne)));
    $calculatedDistance = $earthRadius * $stepTwo;

    return round($calculatedDistance);
}
function getMyDistance($shopGPS)
{
    $arr = explode(',', $shopGPS);
    if (empty($arr[0]) || empty($arr[1]) || !empty($_SESSION['my_location_' . $GLOBALS['mid']])) {
        return '';
    }
    // 无法计算

    $my = explode(',', $_SESSION['my_location_' . $GLOBALS['mid']]);
    return getDistance($arr[0], $arr[1], $my[0], $my[1]);
}
function GPS2Address($location)
{
    $url = 'http://api.map.baidu.com/geocoder/v2/?ak=' . BAIDU_GPS_AK . '&coordtype=wgs84ll&location=' . $location . '&output=json&pois=0';
    $res = wp_file_get_contents($url);
    // dump ( $url );
    $res = json_decode($res, true);
    // dump ( $res );
    return $res['result']['formatted_address'];
}
function xml_to_array($xml)
{
    $reg = "/<(\\w+)[^>]*?>([\\x00-\\xFF]*?)<\\/\\1>/";
    if (preg_match_all($reg, $xml, $matches)) {
        $count = count($matches[0]);
        $arr   = array();
        for ($i = 0; $i < $count; $i++) {
            $key = $matches[1][$i];
            $val = xml_to_array($matches[2][$i]); // 递归
            if (array_key_exists($key, $arr)) {
                if (is_array($arr[$key])) {
                    if (!array_key_exists(0, $arr[$key])) {
                        $arr[$key] = array(
                            $arr[$key],
                        );
                    }
                } else {
                    $arr[$key] = array(
                        $arr[$key],
                    );
                }
                $arr[$key][] = $val;
            } else {
                $arr[$key] = $val;
            }
        }
        return $arr;
    } else {
        return $xml;
    }
}
// Xml 转 数组, 不包括根键
function xmltoarray($xml)
{
    $arr = xml_to_array($xml);
    $key = array_keys($arr);
    return $arr[$key[0]];
}

/**
 * ************************************************************
 *
 * 使用特定function对数组中所有元素做处理
 *
 * @param
 *            string &$array 要处理的字符串
 * @param string $function
 *            要执行的函数
 * @return boolean $apply_to_keys_also 是否也应用到key上
 * @access public
 *
 *         ***********************************************************
 */
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
    static $recursive_counter = 0;
    if (++$recursive_counter > 1000) {
        die('possible deep recursion attack');
    }
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayRecursive($array[$key], $function, $apply_to_keys_also);
        } else {
            $array[$key] = $function($value);
        }

        if ($apply_to_keys_also && is_string($key)) {
            $new_key = $function($key);
            if ($new_key != $key) {
                $array[$new_key] = $array[$key];
                unset($array[$key]);
            }
        }
    }
    $recursive_counter--;
}

/**
 * ************************************************************
 *
 * 将数组转换为JSON字符串（兼容中文）
 *
 * @param array $array
 *            要转换的数组
 * @return string 转换得到的json字符串
 * @access public
 *
 *         ***********************************************************
 */
function JSON($array)
{
    arrayRecursive($array, 'urlencode', true);
    $json = json_encode($array);
    return urldecode($json);
}
/**
 * 短链接功能
 *
 * @param float $long_url
 *            长链接
 * @return string 如果没有微信短链接接口权限或者不成功，就原样返回长链接，否则返回短链接
 */
function short_url($long_url)
{
    $access_token = get_access_token($token);
    if (empty($access_token)) {
        return $long_url;
    }

    $url = 'https://api.weixin.qq.com/cgi-bin/shorturl?access_token=' . $access_token;

    $data['action']   = 'long2short';
    $data['long_url'] = $long_url;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($ch);
    curl_close($ch);
    $res = json_decode($res, true);
    if ($res['errcode'] == 0) {
        return $res['short_url'];
    } else {
        return $long_url;
    }
}
function makeKeyVal($list, $val = 'title', $key = 'id')
{
    foreach ($list as $v) {
        $arr[$v[$key]] = $v[$val];
    }
    return $arr;
}
/**
 * 检查是否是以手机浏览器进入(IN_MOBILE)
 */
function isMobile()
{
    $mobile                    = array();
    static $mobilebrowser_list = 'Mobile|iPhone|Android|WAP|NetFront|JAVA|OperasMini|UCWEB|WindowssCE|Symbian|Series|webOS|SonyEricsson|Sony|BlackBerry|Cellphone|dopod|Nokia|samsung|PalmSource|Xphone|Xda|Smartphone|PIEPlus|MEIZU|MIDP|CLDC';
    // note 获取手机浏览器
    if (preg_match("/$mobilebrowser_list/i", $_SERVER['HTTP_USER_AGENT'], $mobile)) {
        return true;
    } else {
        if (preg_match('/(mozilla|chrome|safari|opera|m3gate|winwap|openwave)/i', $_SERVER['HTTP_USER_AGENT'])) {
            return false;
        } else {
            if ($_GET['mobile'] === 'yes') {
                return true;
            } else {
                return false;
            }
        }
    }
}
function isiPhone()
{
    return strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== false;
}
function isiPad()
{
    return strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') !== false;
}
function isiOS()
{
    return isiPhone() || isiPad();
}
function isAndroid()
{
    return strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false;
}

// 通过服务号获取用户UID
function get_uid_by_openid($init = true, $openid = '')
{
    $info                     = get_token_appinfo();
    empty($openid) && $openid = get_openid();
    if (!$openid) {
        return 0;
    }

    $map['openid'] = $openid;
    $map['token']  = $info['token'];
    $uid           = M('public_follow')->where($map)->getField('uid');

    // addWeixinLog($uid, 'before_init');

    if ($uid) {
        return $uid;
    }

    if (!$init) {
        return 0;
    }

    // 不存在就初始化
    $uid = D('Common/Follow')->init_follow($openid, $map['token']);
    return $uid;
}
/**
 * 用SHA1算法生成安全签名
 */
function getSHA1($array)
{
    // 排序
    sort($array, SORT_STRING);
    $str = implode($array);
    return sha1($str);
}
function getModelByName($name)
{
    $name  = parse_name($name);
    $key   = 'getModelByName_' . $name;
    $model = S($key);
    if (!$model) {
        $model = M('model')->getByName($name);
        S($key, $model, 86400);
    }

    return $model;
}
// 复制目录，目前用于生成素材
function copydir($strSrcDir, $strDstDir)
{
    $dir = opendir($strSrcDir);
    if (!$dir) {
        return false;
    }
    if (!is_dir($strDstDir)) {
        if (!mkdir($strDstDir)) {
            return false;
        }
    }
    while (false !== ($file = readdir($dir))) {
        if ($file == '.' || $file == '..' || $file == '.svn' || $file == '.DS_Store' || $file == '__MACOSX' || $file == 'Thumbs.db' || $file == 'Thumbs.db' || $file == 'info.php') {
            continue;
        }
        if (is_dir($strSrcDir . '/' . $file)) {
            if (!copydir($strSrcDir . '/' . $file, $strDstDir . '/' . $file)) {
                return false;
            }
        } else {
            if (!copy($strSrcDir . '/' . $file, $strDstDir . '/' . $file)) {
                return false;
            }
        }
    }
    closedir($dir);
    return true;
}
/**
 * 获取插件的配置数组
 */
function getAddonConfig($name, $token = '')
{
    static $_config = array();
    if (isset($_config[$name])) {
        return $_config[$name];
    }

    $config = array();

    $token = empty($token) ? get_token() : $token;
    // dump($token);
    if (!empty($token)) {
        $addon_config = get_token_appinfo($token, 'addon_config');
        $addon_config = json_decode($addon_config, true);
        if (isset($addon_config[$name])) {
            $config = $addon_config[$name];
        }

    }
    if (empty($config)) {
        $config = D('Home/Addons')->getInfoByName($name);
        $config = json_decode($config['config'], true);
    }
    if (!$config) {
        $temp_arr = include_once ONETHINK_ADDON_PATH . $name . '/config.php';
        foreach ($temp_arr as $key => $value) {
            $config[$key] = $temp_arr[$key]['value'];
        }
    }
    $_config[$name] = $config;
    return $config;
}
function getPluginConfig($name, $token = '')
{
    static $_config = array();
    if (isset($_config[$name])) {
        return $_config[$name];
    }

    $config = array();

    $token = empty($token) ? get_token() : $token;
    // dump($token);
    if (!empty($token)) {
        $addon_config = get_token_appinfo($token, 'addon_config');
        $addon_config = json_decode($addon_config, true);
        if (isset($addon_config[$name])) {
            $config = $addon_config[$name];
        }

    }
    if (empty($config)) {
        $config = D('Home/Plugins')->getInfoByName($name);
        $config = json_decode($config['config'], true);
    }
    if (!$config) {
        $temp_arr = include_once ONETHINK_PLUGIN_PATH . $name . '/config.php';
        foreach ($temp_arr as $key => $value) {
            $config[$key] = $temp_arr[$key]['value'];
        }
    }
    $_config[$name] = $config;
    return $config;
}
// 删除目录及目录下的所有子目录和文件
function rmdirr($dirname)
{
    if (!file_exists($dirname)) {
        return false;
    }
    if (is_file($dirname) || is_link($dirname)) {
        return unlink($dirname);
    }
    $dir = dir($dirname);
    if ($dir) {
        while (false !== $entry = $dir->read()) {
            if ($entry == '.' || $entry == '..') {
                continue;
            }
            rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
        }
    }
    $dir->close();
    return rmdir($dirname);
}
function wp_money_format($number, $decimals = '2')
{
    return number_format($number, $decimals);
}
// 以GET方式获取数据，替代file_get_contents
function get_data($url, $timeout = 5)
{
    $msg = $flat = '';
    if (strpos($url, 'http://') !== false || strpos($url, 'https://') !== false) {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不检查证书
        $res = curl_exec($ch);

        $flat = curl_errno($ch);
        if ($flat) {
            $msg = curl_error($ch);
        }
        curl_close($ch);
    } else {
        $context = stream_context_create(array(
            'http' => array(
                'timeout' => 30,
            ),
        )); // 超时时间，单位为秒

        $res = file_get_contents($url, 0, $context);
    }
    return $res;
}
// 以POST方式提交数据
function post_data($url, $param, $type = 'json', $return_array = true, $useCert = [])
{
    $type === false && $type = 'json'; // 兼容老版本
    $type === true && $type  = 'file'; // 兼容老版本
    // addWeixinLog($type);
    if ($type == 'json' && is_array($param)) {
        $param = json_encode($param, JSON_UNESCAPED_UNICODE);
    } elseif ($type == 'xml' && is_array($param)) {
        $param = ToXml($param);
    }
    // 初始化curl
    $ch = curl_init();
    // 设置超时
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    if (class_exists('\CURLFile')) {
        // php5.5跟php5.6中的CURLOPT_SAFE_UPLOAD的默认值不同
        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
    } else {
        if (defined('CURLOPT_SAFE_UPLOAD')) {
            curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
        }
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    // 设置header
    if ($type == 'file') {
        $header[] = "content-type: multipart/form-data; charset=UTF-8";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    } elseif ($type == 'xml') {
        curl_setopt($ch, CURLOPT_HEADER, false);
    } else {
        $header[] = "content-type: application/json; charset=UTF-8";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }

    // curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
    // 要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // 使用证书：cert 与 key 分别属于两个.pem文件
    if (isset($useCert['certPath']) && isset($useCert['keyPath'])) {
        curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
        curl_setopt($ch, CURLOPT_SSLCERT, $useCert['certPath']);
        curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
        curl_setopt($ch, CURLOPT_SSLKEY, $useCert['keyPath']);
    }

    $res  = curl_exec($ch);
    $flat = curl_errno($ch);
    $msg  = '';
    if ($flat) {
        $msg = curl_error($ch);
    }
    curl_close($ch);
    if ($flat) {
        return [
            'curl_erron' => $flat,
            'curl_error' => $msg,
        ];
    } else {
        if ($return_array && !empty($res)) {
            $res = $type == 'xml' ? FromXml($res) : json_decode($res, true);
        }

        return $res;
    }
}
/**
 * 输出xml字符
 */
function ToXml($arr = [])
{
    if (!is_array($arr) || count($arr) <= 0) {
        exit("数组数据异常！");
    }

    $xml = "<xml>";
    foreach ($arr as $key => $val) {
        if (is_numeric($val)) {
            $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
        } else {
            $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
        }
    }
    $xml .= "</xml>";
    return $xml;
}

/**
 * 将xml转为array
 */
function FromXml($xml)
{
    if (!$xml) {
        exit("xml数据异常！");
    }
    // 将XML转为array
    $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    return $arr;
}
// 生成签名
function make_sign($paraMap = array(), $partner_key = '')
{
    $buff = "";
    ksort($paraMap);
    $paraMap['key'] = $partner_key;
    foreach ($paraMap as $k => $v) {
        if (null != $v && "null" != $v && '' != $v && "sign" != $k) {
            $buff .= strtolower($k) . "=" . $v . "&";
        }
    }
    $reqPar = '';
    if (strlen($buff) > 0) {
        $reqPar = substr($buff, 0, strlen($buff) - 1);
    }
    return strtoupper(md5($reqPar));
}
// 更具目录获取素材信息
function get_sucai_template_info($template = "default", $addons)
{
    $dir = SITE_PATH . '/SucaiTemplate/' . $addons;
    if (file_exists($dir . '/' . $template . '/info.php')) {
        $info = require_once $dir . '/' . $template . '/info.php';
        return $info;
        exit();
    }
    return null;
}
// //////靓妆//////////
/*
 * 分时分段的抽奖算法 prizeArr 奖品数据，结构是：array(array(prize_id=>1,prize_num=>2),array(prize_id=>2,prize_num=>4),...); start_time 开始的时间戳 end_time 结束的时间戳 event_id 抽奖的活动ID，以确保每个抽奖不冲突
 */
function get_lottery1($prizeArr, $start_time, $end_time, $event_id = 0, $uid = 0, $update = false, $token = '')
{
    if (empty($uid) && $token) {
        // $uid = get_mid ();
        $key = 'function_lottery_' . $token . '_' . $event_id;
    }
    if (!empty($uid)) {
        $key = 'function_lottery_' . $uid . '_' . $event_id;
    }
    $res = S($key);
    if ($res === false || $update) {
        $res = false;
        // 奖品ID组处理
        $count = 0;
        foreach ($prizeArr as $p) {
            for ($i = 0; $i < $p['prize_num']; $i++) {
                $rand[] = $p['prize_id'];
                $count += 1;
            }
        }
        // 奖品ID排序随机
        shuffle($rand);

        // 时间分段
        $start_time < NOW_TIME && $start_time = NOW_TIME; // 如果活动已经开始，以当前时间为开始时间

        $total_time = $end_time - $start_time;

        $section_time = floor($total_time / $count);

        // 生成中奖的数组 结构是：array('中奖的随机时间点'=>'中奖的奖品ID');
        $stime = $start_time;
        for ($j = 0; $j < $count; $j++) {
            $etime       = $stime + $section_time;
            $index       = rand($stime, $etime);
            $res[$index] = $rand[$j];

            $stime = $etime;
        }
        S($key, $res);
    }

    return $res;
}
function del_lottery1($index, $event_id = 0, $uid = 0, $token = '')
{
    if (empty($uid) && $token) {
        $key = 'function_lottery_' . $token . '_' . $event_id;
    }
    if (!empty($uid)) {
        $key = 'function_lottery_' . $uid . '_' . $event_id;
    }

    $res = S($key);
    if ($res === false) {
        return false;
    }

    unset($res[$index]);
    S($key, $res);
}
// ////////////sports//////////////
/*
 * 分时分段的抽奖算法 prizeArr 奖品数据，结构是：array(array(prize_id=>1,prize_num=>2),array(prize_id=>2,prize_num=>4),...); start_time 开始的时间戳 end_time 结束的时间戳 event_id 抽奖的活动ID，以确保每个抽奖不冲突
 */
function get_lottery($prizeArr, $start_time, $end_time, $event_id = 0, $update = false)
{
    $key = 'function_lottery_' . $event_id;
    $res = S($key);
    if ($res === false || $update) {
        $res = false;
        // 奖品ID组处理
        $count_shiwu = 0;
        foreach ($prizeArr['shiwu'] as $p) {
            for ($i = 0; $i < $p['prize_num']; $i++) {
                $rand_shiwu[] = $p['prize_id'];
                $count_shiwu += 1;
            }
        }
        // $count_xuni = 0;
        // foreach ( $prizeArr ['xuni'] as $p ) {
        // for($i = 0; $i < $p ['prize_num']; $i ++) {
        // $rand_xuni [] = $p ['prize_id'];
        // $count_xuni += 1;
        // }
        // }
        // 奖品ID排序随机
        shuffle($rand_shiwu);
        // shuffle ( $rand_xuni );

        // 时间分段
        $start_time < NOW_TIME && $start_time = NOW_TIME; // 如果活动已经开始，以当前时间为开始时间

        $total_time   = $end_time - $start_time;
        $section_time = floor($total_time / $count_shiwu);
        // 生成中奖的数组 结构是：array('中奖的随机时间点'=>'中奖的奖品ID');
        $stime = $start_time;
        for ($j = 0; $j < $count_shiwu; $j++) {

            $etime = $stime + $section_time;
            // $index = rand ( $stime, $etime );
            // $res [$index] = $rand_shiwu [$j];
            $shiwu_prize[$etime] = $rand_shiwu[$j];
            $stime               = $etime;
        }
        $res['shiwu'] = $shiwu_prize;
        // $res ['xuni'] = $rand_xuni;
        S($key, $res);
    }

    return $res;
}
// 根据概率随机抽取积分奖品
function get_jifen_lottery($proArr)
{
    $allPro = array_sum($proArr);
    $result = 0;
    foreach ($proArr as $k => $p) {
        $randNum = mt_rand(1, $allPro);
        if ($randNum <= $p) {
            $result = $k;
        } else {
            $allPro -= $p;
        }
    }
    unset($proArr);
    return $result;
}
function del_lottery($index, $event_id = 0, $delkeyname = 'shiwu')
{
    $key = 'function_lottery_' . $event_id;
    $res = S($key);
    if ($res === false) {
        return false;
    }

    unset($res[$delkeyname][$index]);
    S($key, $res);
}
function lists_msubstr($str)
{
    return msubstr($str, 0, 30);
}
function parseComment($comment, $file = 'lzwg', $width = '40')
{
    preg_match_all('/\[[a-zA-Z0-9]+\]/', $comment, $res);
    $faceData = $res[0];
    if (count($faceData) != 0) {
        foreach ($faceData as $v) {
            $faceName       = substr($v, 1, strlen($v) - 2);
            $faceUrl        = '<img width=' . $width . ' src="' . SITE_URL . '/Public/static/face/' . $file . '/' . $faceName . '.png"/>';
            $replaceArr[$v] = $faceUrl;
        }
    }
    if ($replaceArr) {
        $comment = strtr($comment, $replaceArr);
    }
    return $comment;
}
/**
 * 系统非常规MD5加密方法
 *
 * @param string $str
 *            要加密的字符串
 * @return string
 */
function think_weiphp_md5($str, $key = '')
{
    empty($key) && $key = C('DATA_AUTH_KEY');
    return '' === $str ? '' : md5(sha1($str) . $key);
}
/**
 * 检测验证码
 *
 * @param integer $id
 *            验证码ID
 * @return boolean 检测结果
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function check_verify($code, $id = 1)
{
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
}
// 权限检查
function checkRule($rule = '', $uid = '')
{
    static $Auth = null;
    if (!$Auth) {
        $Auth = new \Think\Auth();
    }
    return $Auth->checkRule($rule, $uid);
}
// 微信端的错误码转中文解释
function error_msg($return, $more_tips = '')
{
    $msg = array(
        '-1'      => '系统繁忙，此时请开发者稍候再试',
        '0'       => '请求成功',
        '40001'   => '获取access_token时AppSecret错误，或者access_token无效。请开发者认真比对AppSecret的正确性，或查看是否正在为恰当的公众号调用接口',
        '40002'   => '不合法的凭证类型',
        '40003'   => '不合法的OpenID，请开发者确认OpenID（该用户）是否已关注公众号，或是否是其他公众号的OpenID',
        '40004'   => '不合法的媒体文件类型',
        '40005'   => '不合法的文件类型',
        '40006'   => '不合法的文件大小',
        '40007'   => '不合法的媒体文件id',
        '40008'   => '不合法的消息类型',
        '40009'   => '不合法的图片文件大小',
        '40010'   => '不合法的语音文件大小',
        '40011'   => '不合法的视频文件大小',
        '40012'   => '不合法的缩略图文件大小',
        '40013'   => '不合法的AppID，请开发者检查AppID的正确性，避免异常字符，注意大小写',
        '40014'   => '不合法的access_token，请开发者认真比对access_token的有效性（如是否过期），或查看是否正在为恰当的公众号调用接口',
        '40015'   => '不合法的菜单类型',
        '40016'   => '不合法的按钮个数',
        '40017'   => '不合法的按钮个数',
        '40018'   => '不合法的按钮名字长度',
        '40019'   => '不合法的按钮KEY长度',
        '40020'   => '不合法的按钮URL长度',
        '40021'   => '不合法的菜单版本号',
        '40022'   => '不合法的子菜单级数',
        '40023'   => '不合法的子菜单按钮个数',
        '40024'   => '不合法的子菜单按钮类型',
        '40025'   => '不合法的子菜单按钮名字长度',
        '40026'   => '不合法的子菜单按钮KEY长度',
        '40027'   => '不合法的子菜单按钮URL长度',
        '40028'   => '不合法的自定义菜单使用用户',
        '40029'   => '不合法的oauth_code',
        '40030'   => '不合法的refresh_token',
        '40031'   => '不合法的openid列表',
        '40032'   => '不合法的openid列表长度',
        '40033'   => '不合法的请求字符，不能包含\uxxxx格式的字符',
        '40035'   => '不合法的参数',
        '40038'   => '不合法的请求格式',
        '40039'   => '不合法的URL长度',
        '40050'   => '不合法的分组id',
        '40051'   => '分组名字不合法',
        '40117'   => '分组名字不合法',
        '40118'   => 'media_id大小不合法',
        '40119'   => 'button类型错误',
        '40120'   => 'button类型错误',
        '40121'   => '不合法的media_id类型',
        '40132'   => '微信号不合法',
        '40137'   => '不支持的图片格式',
        '41001'   => '缺少access_token参数',
        '41002'   => '缺少appid参数',
        '41003'   => '缺少refresh_token参数',
        '41004'   => '缺少secret参数',
        '41005'   => '缺少多媒体文件数据',
        '41006'   => '缺少media_id参数',
        '41007'   => '缺少子菜单数据',
        '41008'   => '缺少oauth code',
        '41009'   => '缺少openid',
        '42001'   => 'access_token超时，请检查access_token的有效期，请参考基础支持-获取access_token中，对access_token的详细机制说明',
        '42002'   => 'refresh_token超时',
        '42003'   => 'oauth_code超时',
        '43001'   => '需要GET请求',
        '43002'   => '需要POST请求',
        '43003'   => '需要HTTPS请求',
        '43004'   => '需要接收者关注',
        '43005'   => '需要好友关系',
        '44001'   => '多媒体文件为空',
        '44002'   => 'POST的数据包为空',
        '44003'   => '图文消息内容为空',
        '44004'   => '文本消息内容为空',
        '45001'   => '多媒体文件大小超过限制',
        '45002'   => '消息内容超过限制',
        '45003'   => '标题字段超过限制',
        '45004'   => '描述字段超过限制',
        '45005'   => '链接字段超过限制',
        '45006'   => '图片链接字段超过限制',
        '45007'   => '语音播放时间超过限制',
        '45008'   => '图文消息超过限制',
        '45009'   => '接口调用超过限制',
        '45010'   => '创建菜单个数超过限制',
        '45015'   => '回复时间超过限制',
        '45016'   => '系统分组，不允许修改',
        '45017'   => '分组名字过长',
        '45018'   => '分组数量超过上限',
        '46001'   => '不存在媒体数据',
        '46002'   => '不存在的菜单版本',
        '46003'   => '不存在的菜单数据',
        '46004'   => '不存在的用户',
        '47001'   => '解析JSON/XML内容错误',
        '48001'   => 'api功能未授权，请确认公众号已获得该接口，可以在公众平台官网-开发者中心页中查看接口权限',
        '50001'   => '用户未授权该api',
        '50002'   => '用户受限，可能是违规后接口被封禁',
        '61451'   => '参数错误(invalid parameter)',
        '61452'   => '无效客服账号(invalid kf_account)',
        '61453'   => '客服帐号已存在(kf_account exsited)',
        '61454'   => '客服帐号名长度超过限制(仅允许10个英文字符，不包括@及@后的公众号的微信号)(invalid kf_acount length)',
        '61455'   => '客服帐号名包含非法字符(仅允许英文+数字)(illegal character in kf_account)',
        '61456'   => '客服帐号个数超过限制(10个客服账号)(kf_account count exceeded)',
        '61457'   => '无效头像文件类型(invalid file type)',
        '61450'   => '系统错误(system error)',
        '61500'   => '日期格式错误',
        '61501'   => '日期范围错误',
        '9001001' => 'POST数据参数不合法',
        '9001002' => '远端服务不可用',
        '9001003' => 'Ticket不合法',
        '9001004' => '获取摇周边用户信息失败',
        '9001005' => '获取商户信息失败',
        '9001006' => '获取OpenID失败',
        '9001007' => '上传文件缺失',
        '9001008' => '上传素材的文件类型不合法',
        '9001009' => '上传素材的文件尺寸不合法',
        '9001010' => '上传失败',
        '9001020' => '帐号不合法',
        '9001021' => '已有设备激活率低于50%，不能新增设备',
        '9001022' => '设备申请数不合法，必须为大于0的数字',
        '9001023' => '已存在审核中的设备ID申请',
        '9001024' => '一次查询设备ID数量不能超过50',
        '9001025' => '设备ID不合法',
        '9001026' => '页面ID不合法',
        '9001027' => '页面参数不合法',
        '9001028' => '一次删除页面ID数量不能超过10',
        '9001029' => '页面已应用在设备中，请先解除应用关系再删除',
        '9001030' => '一次查询页面ID数量不能超过50',
        '9001031' => '时间区间不合法',
        '9001032' => '保存设备与页面的绑定关系参数错误',
        '9001033' => '门店ID不合法',
        '9001034' => '设备备注信息过长',
        '9001035' => '设备申请参数不合法',
        '9001036' => '查询起始值begin不合法',
    );

    if ($more_tips) {
        $res = $more_tips . ': ';
    } else {
        $res = '';
    }
    if (isset($msg[$return['errcode']])) {
        $res .= $msg[$return['errcode']];
    } else {
        $res .= $return['errmsg'];
    }

    $res .= ', 返回码：' . $return['errcode'];

    return $res;
}
/* yqx */
function virifylocal()
{
    define('VIRIFY', true);
    define('HTML_VESION', 'index3_6');
    C('JS_VISION', 3.6);
}
/**
 * 获取随机字符串
 *
 * @param number $length
 * @return string
 */
function createNonceStr($length = 16)
{
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str   = "";
    for ($i = 0; $i < $length; $i++) {
        $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
}
function checkAllow_nums()
{
    /*
     * $_scene = M('scene'); $where['uid'] = intval(session('mid')); $where['delete_int'] =0; $count = $_scene->where($where)->count(); $allowNum=M('user')->where($where)->getField('allow_nums');
     */
    $allowNum = 20;
    if ($count >= $allowNum) {
        echo '{"success":true,"code":1006,"msg":' . $allowNum . ',"obj":null,"map":null,"list":null}';
        exit();
    }
    return true;
}
function randorderno($length = 10, $type = 0)
{
    $arr = array(
        1 => "3425678934567892345678934567892",
        2 => "ABCDEFGHJKLMNPQRSTUVWXY",
    );
    $code = '';
    if ($type == 0) {
        array_pop($arr);
        $string = implode("", $arr);
    } else if ($type == "-1") {
        $string = implode("", $arr);
    } else {
        $string = $arr[$type];
    }
    $count = strlen($string) - 1;
    for ($i = 0; $i < $length; $i++) {
        $str[$i] = $string[rand(0, $count)];
        $code .= $str[$i];
    }
    return $code;
}
function aboutaa()
{
    virifylocal();
}
// 泛域名情况下的域名替换
function chang_domain($url, $domain)
{
    if (!C('DIV_DOMAIN') || is_numeric($domain) || SITE_DOMAIN == 'localhost' || empty($domain)) {
        return $url;
    }

    $arr = explode('.', SITE_DOMAIN);
    if (count($arr) < 3) {
        // 顶级域名
        $new_site_domain = $domain . '.' . SITE_DOMAIN;
    } else {
        $arr[0]          = $domain;
        $new_site_domain = implode('.', $arr);
    }

    $url = str_replace(SITE_DOMAIN, $new_site_domain, $url);

    return $url;
}
// 获取当前网址的顶级域名
function top_domain()
{
    if (strpos(SITE_DOMAIN, 'weiphp.cn') !== false) {
        return '.weiphp.cn';
    } else {
        return '.oftenchat.cn';
    }
}
/*
// 处理带Emoji的数据，type=0表示写入数据库前的emoji转为HTML，为1时表示HTML转为emoji码
function deal_emoji($msg, $type = 1) {
if ($type == 0) {
$msg = urlencode ( $msg );
$msg = json_encode ( $msg );
} else {

$msg = preg_replace ( "#\\\u([0-9a-f]+)#ie", "iconv('UCS-2','UTF-8', pack('H4', '\\1'))", $msg );

// $msg = preg_replace("#(\\\ue[0-9a-f]{3})#ie", "addslashes('\\1')",$msg);

$msg = urldecode ( $msg );
// $msg = json_decode ( $msg );
// dump($msg);
$msg = str_replace ( '"', "", $msg );
// dump($msg);exit;
if ($txt !== null) {
$msg = $txt;
}
}

return $msg;
}
 */

// 处理带Emoji的数据，type=0表示写入数据库前的emoji转为HTML，为1时表示HTML转为emoji码
function deal_emoji($msg, $type = 1)
{
    if ($type == 0) {
        $msg = urlencode($msg);
        $msg = json_encode($msg, JSON_UNESCAPED_UNICODE);
    } else {

//        $msg = preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2','UTF-8', pack('H4', '\\1'))", $msg);

        // $msg = preg_replace("#(\\\ue[0-9a-f]{3})#ie", "addslashes('\\1')",$msg);
        if (strpos($msg, '"') === false) {
            return $msg;
        }

        $msg = urldecode($msg);
        $msg = json_decode($msg);
        // dump($msg);
        //        $msg = str_replace('"', "", $msg);
        // dump($msg);exit;
    }

    return $msg;
}
// 从微信下载临时图片文件
// $media_id 媒体文件ID
function down_media($media_id)
{
    $savePath = SITE_PATH . '/Uploads/Picture/' . time_format(NOW_TIME, 'Y-m-d');
    mkdirs($savePath);
    // 获取图片URL

    $url        = 'http://api.weixin.qq.com/cgi-bin/media/get?access_token=' . get_access_token() . '&media_id=' . $media_id;
    $picContent = wp_file_get_contents($url);
    $picjson    = json_decode($picContent, true);
    if (isset($picjson['errcode']) && $picjson['errcode'] != 0) {
        return 0;
    }

    $picName  = $media_id . '.jpg';
    $picPath  = $savePath . '/' . $picName;
    $res      = file_put_contents($picPath, $picContent);
    $cover_id = 0;
    if ($res) {
        // 保存记录，添加到picture表里，获取coverid
        $url = U('File/uploadPicture', array(
            'session_id' => session_id(),
        ));
        $_FILES['download'] = array(
            'name'     => $picName,
            'type'     => 'application/octet-stream',
            'tmp_name' => $picPath,
            'size'     => $res,
            'error'    => 0,
        );
        $Picture    = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info       = $Picture->upload($_FILES, C('PICTURE_UPLOAD'), C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
        $cover_id   = $info['download']['id'];
        unlink($picPath);
    }
    return $cover_id;
}
function outputCurl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

// 上传多媒体文体
function upload_media($path, $type = 'image')
{
    $url           = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . get_access_token();
    $param['type'] = $type;
    if (class_exists('\CURLFile')) {
        // 关键是判断curlfile,官网推荐php5.5或更高的版本使用curlfile来实例文件
        $param['media'] = new \CURLFile(realpath($path));
    } else {
        $param['media'] = '@' . realpath($path);
    }
    $res = post_data($url, $param, true);
    if (isset($res['errcode']) && $res['errcode'] != 0) {
        $this->error(error_msg($res, '图片上传'));
        exit();
    }
    return $res['media_id'];
}
// 短信锁
function smsLock($tel, $time = 60)
{

    // 锁定时间
    $locktime = $time;
    // 创建目录
    if (!is_dir(SITE_PATH . '/Data/smslock/')) {
        mkdir(SITE_PATH . '/Data/smslock/', 0777, true);
    }

    // 锁文件
    $lockfile = SITE_PATH . '/Data/smslock/' . $tel . '.txt';
    // 文件验证时间间隔
    if (file_exists($lockfile)) {
        if ((time() - filemtime($lockfile)) < $locktime) {
            return false;
        } else {
            $k = fopen($lockfile, "a+");
            fwrite($k, ',' . time());
            fclose($k);
        }
    } else {
        touch($lockfile);
    }
    return true;
}
// 下载永久素材
function do_down_image($media_id, $picUrl = '')
{
    $savePath = SITE_PATH . '/Uploads/Picture/' . time_format(NOW_TIME, 'Y-m-d');
    mkdirs($savePath);
    if (empty($picUrl)) {
        // 获取图片URL
        $url               = 'https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=' . get_access_token();
        $param['media_id'] = $media_id;
        $picContent        = post_data($url, $param, false, false);
        $picjson           = json_decode($picContent, true);
        if (isset($picjson['errcode']) && $picjson['errcode'] != 0) {
            // $this->error ( error_msg ( $picjson, '下载图片' ) );
            return 0;
            exit();
        }
        // dump($picContent);
        // dump($picjson);
        // if ($picContent){
        $picName = NOW_TIME . '.jpg';
        $picPath = $savePath . '/' . $picName;
        $res     = file_put_contents($picPath, $picContent);
        // }
    } else {
        $content = wp_file_get_contents($picUrl);
        // 获取图片扩展名
        $picExt = substr($picUrl, strrpos($picUrl, '=') + 1);
        // $picExt=='jpeg'
        if (empty($picExt) || $picExt == 'jpeg') {
            $picExt = 'jpg';
        }
        $picName = NOW_TIME . '.' . $picExt;
        $picPath = $savePath . '/' . $picName;
        $res     = file_put_contents($picPath, $content);
        if (!$res) {
            // $this->error ( '远程图片下载失败' );
            // exit ();
            return 0;
            exit();
        }
    }
    $cover_id = 0;
    if ($res) {
        // 保存记录，添加到picture表里，获取coverid
        $url = U('File/uploadPicture', array(
            'session_id' => session_id(),
        ));
        $_FILES['download'] = array(
            'name'     => $picName,
            'type'     => 'application/octet-stream',
            'tmp_name' => $picPath,
            'size'     => $res,
            'error'    => 0,
        );
        $Picture    = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info       = $Picture->upload($_FILES, C('PICTURE_UPLOAD'), C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
        $cover_id   = $info['download']['id'];
        unlink($picPath);
    }
    return $cover_id;
}
// 下载临时语言、视频素材
function down_file_media($media_id, $type = 'voice')
{
    $savePath = SITE_PATH . '/Uploads/Download/' . time_format(NOW_TIME, 'Y-m-d');
    mkdirs($savePath);
    // 获取图片URL
    $url        = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . get_access_token() . '&media_id=' . $media_id;
    $picContent = outputCurl($url);
    $picjson    = json_decode($picContent, true);
    if (isset($picjson['errcode']) && $picjson['errcode'] != 0) {
        return 0;
    }
    if ($type == 'voice') {
        $ext = 'mp3';
    } else if ($type == 'video') {
        $ext = 'mp4';
    }
    if ($picContent) {
        $picName = NOW_TIME . '.' . $ext;
        $picPath = $savePath . '/' . $picName;
        $res     = file_put_contents($picPath, $picContent);
    }
    $cover_id = 0;
    if ($res) {
        // 保存记录，添加到picture表里，获取coverid
        $_FILES['download'] = array(
            'name'     => $picName,
            'type'     => 'application/octet-stream',
            'tmp_name' => $picPath,
            'size'     => $res,
            'error'    => 0,
        );
        $File        = D('Home/File');
        $file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
        $info        = $File->upload($_FILES, C('DOWNLOAD_UPLOAD'), C('DOWNLOAD_UPLOAD_DRIVER'), C("UPLOAD_{$file_driver}_CONFIG"));
        $cover_id    = $info['download']['id'];
        unlink($picPath);
    }
    return $cover_id;
}

// 二维数组根据键排序
function array_sort($arr, $keys, $type = 'desc')
{
    $keysvalue = $new_array = array();
    foreach ($arr as $k => $v) {
        $keysvalue[$k] = $v[$keys];
    }
    if ($type == 'asc') {
        asort($keysvalue);
    } else {
        arsort($keysvalue);
    }
    reset($keysvalue);
    foreach ($keysvalue as $k => $v) {
        $new_array[$k] = $arr[$k];
    }
    return $new_array;
}
function test_mem_cache()
{
    $key = 'test_mem_cache';
    $res = S($key);
    if ($res !== false) {
        return $res;
    }

    S($key, NOW_TIME, 7200);
    return NOW_TIME;
}

// 转换得到含emoji表情的代码 注意引入css文件
function parseHtmlemoji($text)
{
    vendor("emoji");
    $tmpStr = json_encode($text);
    $tmpStr = preg_replace("#(\\\ue[0-9a-f]{3})#ie", "addslashes('\\1')", $tmpStr);
    $text   = json_decode($tmpStr);
    preg_match_all("#u([0-9a-f]{4})+#iUs", $text, $rs);
    if (empty($rs[1])) {
        return $text;
    }
    foreach ($rs[1] as $v) {
        $test_iphone = '0x' . trim(strtoupper($v));
        $test_iphone = $test_iphone + 0;
        $utbytes     = utf8_bytes($test_iphone);
        $emji        = emoji_softbank_to_unified($utbytes);
        $t           = emoji_unified_to_html($emji);
        $text        = str_replace("\u$v", $t, $text);
    }
    return $text;
}
function utf8_bytes($cp)
{
    if ($cp > 0x10000) {
        // 4 bytes
        return chr(0xF0 | (($cp & 0x1C0000) >> 18)) . chr(0x80 | (($cp & 0x3F000) >> 12)) . chr(0x80 | (($cp & 0xFC0) >> 6)) . chr(0x80 | ($cp & 0x3F));
    } else if ($cp > 0x800) {
        // 3 bytes
        return chr(0xE0 | (($cp & 0xF000) >> 12)) . chr(0x80 | (($cp & 0xFC0) >> 6)) . chr(0x80 | ($cp & 0x3F));
    } else if ($cp > 0x80) {
        // 2 bytes
        return chr(0xC0 | (($cp & 0x7C0) >> 6)) . chr(0x80 | ($cp & 0x3F));
    } else {
        // 1 byte
        return chr($cp);
    }
}
function curl_post($url, $data = null)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); // 设置访问的url地址
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36');
    /*
     * if($data){
     * curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
     * curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
     * curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
     *
     * }
     */
    $data && curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);
    if (curl_errno($ch)) {
        return curl_error($ch);
    }
    curl_close($ch);
    return $tmpInfo;
}
function matchImages($content = '')
{
    $src = array();
    preg_match_all('/<img.*src=\s*[\'"](.*)[\s>\'"]/isU', $content, $src);
    if (count($src[1]) > 0) {
        foreach ($src[1] as $v) {
            $images[] = trim($v, "\"'"); // 删除首尾的引号 ' "
        }
        return $images;
    } else {
        return false;
    }
}
function getEditorImages($content)
{
    preg_match_all('/<img.*src=\s*[\'"](.*)[\s>\'"]/isU', $content, $matchs);
    $image = '';
    foreach ($matchs[1] as $match) {
        $isFace = strpos($match, '/emotion/') === false ? false : true;
        if ($isFace) {
            continue;
        }
        if (preg_match('/http:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is', $match) && !$isFace) {
            $image = $match;
        } else if (!$isFace) {
            $url   = implode('/', array_slice(explode('/', $match), -4));
            $image = getImageUrl($url, 200, 200, true);
        }
        break;
    }

    return $image;
}
function matchReplaceImages($content = '')
{
    $image = preg_replace_callback('/<img.*src=\s*[\'"](.*)[\s>\'"]/isU', "matchReplaceImagesOnce", $content);
    return $image;
}
function matchReplaceImagesOnce($matches)
{
    $matches[1] = str_replace('"', '', $matches[1]);
    return sprintf("<a class='thickbox'  href='%s'>%s</a>", $matches[1], $matches[0]);
}
/**
 * 获取字符串的长度
 *
 * 计算时, 汉字或全角字符占1个长度, 英文字符占0.5个长度
 *
 * @param string $str
 * @param boolean $filter
 *            是否过滤html标签
 * @return int 字符串的长度
 */
function get_str_length($str, $filter = false)
{
    if ($filter) {
        $str = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
        $str = strip_tags($str);
    }
    return (strlen($str) + mb_strlen($str, 'UTF8')) / 4;
}
/**
 * 检查表单是否已锁定
 *
 * @return boolean 表单已锁定时返回true, 否则返回false
 */
function isSubmitLocked()
{
    return isset($_SESSION['LOCK_SUBMIT_TIME']) && intval($_SESSION['LOCK_SUBMIT_TIME']) > time();
}
/**
 * 锁定表单
 *
 * @param int $life_time
 *            表单锁的有效时间(秒). 如果有效时间内未解锁, 表单锁自动失效.
 * @return boolean 成功锁定时返回true, 表单锁已存在时返回false
 */
function lockSubmit($life_time = null)
{
    if (isset($_SESSION['LOCK_SUBMIT_TIME']) && intval($_SESSION['LOCK_SUBMIT_TIME']) > time()) {
        return false;
    } else {
        $life_time                    = $life_time ? $life_time : 10;
        $_SESSION['LOCK_SUBMIT_TIME'] = time() + intval($life_time);
        return true;
    }
}
/**
 * 表单解锁
 *
 * @return void
 */
function unlockSubmit()
{
    unset($_SESSION['LOCK_SUBMIT_TIME']);
}
/**
 * 记录自动检测过程数据
 */
function addAutoCheckLog($na = '', $msg = '', $token = '')
{
    $data['na']    = empty($na) ? I('na') : $na;
    $data['msg']   = $msg;
    $data['token'] = empty($token) ? get_token() : $token;

    // dump ( $data );
    $res = M('public_check')->add($data);
    // dump ( M ( 'public_check' )->getLastSql() );
}

/**
 * 通用奖品选择器
 * 显示奖品信息
 */
function get_prize_detail($prizeValue)
{
    $data      = array();
    $prizeData = explode(',', $prizeValue);
    foreach ($prizeData as $key => $value) {
        $keyArr = explode(':', $value);
        if (empty($keyArr[0])) {
            continue;
        }

        $title       = '';
        $typeName    = '';
        $imgurl      = '';
        $total_count = 0;
        $num         = $keyArr[2];
        if ($keyArr[0] == 'coupon') {
            $pdata       = D('Addons://Coupon/Coupon')->getInfo($keyArr[1]);
            $typeName    = '优惠卷';
            $title       = $pdata['title'];
            $imgurl      = $pdata['background'];
            $total_count = $pdata['num'];
        } elseif ($keyArr[0] == 'shopCoupon' && is_install("ShopCoupon")) {
            $pdata       = D('Addons://ShopCoupon/ShopCoupon')->getInfo($keyArr[1]);
            $typeName    = '代金卷';
            $title       = $pdata['title'];
            $total_count = $pdata['num'];
        } elseif ($keyArr[0] == 'realPrize') {
            $pdata       = D('Addons://RealPrize/RealPrize')->getInfo($keyArr[1]);
            $typeName    = '实物奖励';
            $title       = $pdata['prize_name'];
            $imgurl      = $pdata['prize_image'];
            $total_count = $pdata['prize_count'];
        } elseif ($keyArr[0] == 'cardVouchers') {
            $pdata    = D('Addons://CardVouchers/CardVouchers')->getInfo($keyArr[1]);
            $typeName = '微信卡卷';
            $title    = $pdata['title'];
            $imgurl   = $pdata['background'];
        } elseif ($keyArr[0] == 'redBag') {
            $pdata       = D('Addons://RedBag/RedBag')->getInfo($keyArr[1]);
            $typeName    = '微信红包';
            $title       = $pdata['act_name'];
            $total_count = $pdata['total_num'];
        } elseif ($keyArr[0] == 'points') {
            $typeName    = '积分';
            $title       = '积分';
            $num         = $keyArr[3];
            $total_count = $keyArr[2]; // 奖励的积分数
        }

        $data['title'][$key]       = $title;
        $data['id'][$key]          = $keyArr[1];
        $data['typeName'][$key]    = $typeName;
        $data['img'][$key]         = get_cover_url($imgurl);
        $data['num'][$key]         = $num;
        $data['total_count'][$key] = $total_count;
        $data['type'][$key]        = $keyArr[0];
    }
    return $data;
}
// 文件名
/**
 * 获取缩略图
 *
 * @param unknown_type $filename
 *            原图路劲、url
 * @param unknown_type $width
 *            宽度
 * @param unknown_type $height
 *            高
 * @param unknown_type $cut
 *            是否切割 默认不切割
 * @return string
 */
function getThumbImage($filename, $width = 100, $height = 'auto', $cut = false, $replace = false, $redirect = false)
{
    return $filename; // 待完善
}
// 判断微信插件是否已经安装
function is_install($addon_name)
{
    $list = D('Home/Addons')->getList();
    return isset($list[$addon_name]);
}

/*
 * 上传图片到微信获取url
 * 上传图文消息内的图片获取URL 请注意，本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。
 * 图片仅支持jpg/png格式，大小必须在1MB以下。
 */
function uploadimg($path)
{
    if (preg_match('#^(http|https)://mmbiz.qpic.cn/#i', $path)) {
        return $path;
    }
    $filePath = '';
    if (!file_exists($path)) {
        $filePath = './Uploads/' . think_weiphp_md5($path) . '.jpg';
        getImg($path, $filePath);
        $path = $filePath;
    }
    $url           = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=' . get_access_token();
    $param['type'] = 'image';
    if (class_exists('\CURLFile')) {
        // 关键是判断curlfile,官网推荐php5.5或更高的版本使用curlfile来实例文件
        $param['media'] = new \CURLFile(realpath($path));
    } else {
        $param['media'] = '@' . realpath($path);
    }
    $res = post_data($url, $param, true);
    unlink($filePath); //删除本地创建的图片
    return empty($res['url']) ? '' : $res['url'];
}

/*
 * @通过curl方式获取指定的图片到本地
 * @ 完整的图片地址
 * @ 要存储的文件名
 */
function getImg($url = "", $filename = "")
{
    // 去除URL连接上面可能的引号
    // $url = preg_replace( '/(?:^['"]+|['"/]+$)/', '', $url );
    $hander = curl_init();
    $fp     = fopen($filename, 'wb');
    curl_setopt($hander, CURLOPT_URL, $url);
    curl_setopt($hander, CURLOPT_FILE, $fp);
    curl_setopt($hander, CURLOPT_HEADER, 0);
    curl_setopt($hander, CURLOPT_FOLLOWLOCATION, 1);
    // curl_setopt($hander,CURLOPT_RETURNTRANSFER,false);//以数据流的方式返回数据,当为false是直接显示出来
    curl_setopt($hander, CURLOPT_TIMEOUT, 10);
    curl_exec($hander);
    curl_close($hander);
    fclose($fp);
    return true;
}

// 获取显示确定规格图片
/*
 * http://img.baidu.com/hi/jx2/j_0002.gif
 * http://img1.gtimg.com/auto/pics/hv1/156/84/2125/138199701.jpg
 * /Uploads/Editor/gh_dd85ac50d2dd/2016-08-26/57bfa4a23fba5.png
 */
function mk_rule_image($imgurl, $w, $h)
{
    if (preg_match('#^/Uploads/Picture/#i', $imgurl)) {
        // 内部图片
        $imgurl      = '.' . $imgurl;
        $filename    = basename($imgurl);
        $filename_ex = explode('.', $filename);
        $dirname     = dirname($imgurl);
        $dirname_new = $dirname . '/' . $filename_ex[0] . "_$w" . "X$h." . $filename_ex[1];
        if (file_exists($dirname_new)) {
            return str_replace('./Uploads', SITE_URL . '/Uploads', $dirname_new);
        }

        file_exists($imgurl) && $imginfo = getimagesize($imgurl); // 图片存在并获取到信息

        if ($imginfo) {
            // 规格图片存在
            if ($imginfo[0] > $w || $imginfo['1'] > $h) {
                $img_model || $img_model = new \Think\Image();
                // 生成缩略图
                $re = $img_model->open($imgurl);

                $res = $img_model->thumb($w, $h)->save($dirname_new);
            } else {
                return SITE_URL . $imgurl;
            }
        }
        return str_replace('./Uploads', SITE_URL . '/Uploads', $dirname_new);
    }
    if (preg_match('#^(http|https)://#i', $imgurl)) {
        // 外部
        $imgurl1 = $imgurl;
        $imginfo = getimagesize($imgurl); // 图片存在并获取到信息
        // dump($imginfo);
        $url_info    = parse_url($imgurl);
        $filename    = basename($url_info['path']);
        $filename_ex = explode('.', $filename);
        $dirname     = './Uploads/Picture';
        $dirname_new = $dirname . '/' . think_weiphp_md5($filename_ex[0] . $url_info['query']) . "_$w" . "X$h." . 'jpg'; // $filename_ex[1];
        $imgurl      = SITE_URL . '/Uploads/Picture/' . think_weiphp_md5($filename_ex[0] . $url_info['query']) . "_$w" . "X$h." . 'jpg';
        if (file_exists($dirname_new)) {
            return $imgurl;
        }
        if ($imginfo) {
            // 规格图片存在
            if ($imginfo[0] > $w || $imginfo[1] > $h) {
                $img_model || $img_model = new \Think\Image();

                $save_filename = './Uploads/Picture/' . $filename;
                $res           = getImg($imgurl1, $save_filename);

                $re = $img_model->open($save_filename);

                $res = $img_model->thumb($w, $h)->save($dirname_new);
                unlink($save_filename);
            } else {
                getImg($imgurl1, $dirname_new);
                $imgurl = SITE_URL . $dirname_new;
            }
        }
        return $imgurl;
    }
    return SITE_URL . $imgurl;
}

function get_code_to_session($code)
{

    $appInfo = get_token_appinfo(); //appid secret
    //    dump($appInfo);
    //    addWeixinLog($appInfo);

    if ($appInfo['is_bind']) {
//        $access_token = get_access_token();
        $access_token    = D('Addons://PublicBind/PublicBind')->_get_component_access_token();
        $component_appid = C('COMPONENT_APPID') ? C('COMPONENT_APPID') : 'wxb8b0fcba2892e776';
//        echo $access_token;
        //        echo $component_appid;
        //        dump($appInfo);
        //        exit;
        $url = 'https://api.weixin.qq.com/sns/component/jscode2session?appid=' . $appInfo['appid'] . '&js_code=' . $code
            . '&grant_type=authorization_code&component_appid=' . $component_appid . '&component_access_token=' . $access_token;
//        addWeixinLog($url, 'url');
    } else {
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appInfo['appid'] . '&secret=' . $appInfo['secret'] . '&js_code=' . $code . '&grant_type=authorization_code';
    }

    // $output_array = json_decode($output,true);
    $jscode2session = file_get_contents($url);
//    addWeixinLog($jscode2session, 'result');

    return json_decode($jscode2session, true);
}

function get_code_to_session_by_key($key)
{
    $opse = get_wxapp_session($key);
    return $opse['session_key'];
}

function get_server_session($code)
{
    $jscode2session = get_code_to_session($code);
    if ($jscode2session['errcode']) {
        return $jscode2session;
    }

    $jscode2session['cTime']      = time();
    $session                      = session_id();
    $jscode2session['session']    = $session;
    $jscode2session['expires_in'] = time() + 7200;
    M('wxapp_code2session')->add($jscode2session);
    $expirein = $jscode2session['expires_in'];
    unset($jscode2session['expires_in']);
    $value = json_encode($jscode2session);

    S($session, $value, $expirein);
    $data['session'] = $session;
    $data['openid']  = $jscode2session['openid'];
    // 判断是否需要初始化用户数据

    $openid = $jscode2session['openid'];
    if ($follow = M('public_follow')->where(array('openid' => $openid))->find()) {
        $data['init']                   = false;
        $userinfo                       = get_userinfo($follow['uid']);
        $data['userInfo']['nickname']   = $userinfo['nickname'];
        $data['userInfo']['headimgurl'] = $userinfo['headimgurl'];
    } elseif (isset($jscode2session['unionid']) && $jscode2session['unionid']) {
        if ($user = M('user')->where(array('unionid' => $jscode2session['unionid']))->find()) {
            $follow = array(
                'openid' => $jscode2session['openid'],
                'token'  => $this->token,
                'uid'    => $user['uid'],
                'remark' => 'wxapp',
            );
            D('Common/User')->initPublicFollow($follow);
            $userinfo                       = get_userinfo($follow['uid']);
            $data['userInfo']['nickname']   = $userinfo['nickname'];
            $data['userInfo']['headimgurl'] = $userinfo['headimgurl'];
        } else {
            $data['init'] = true;
        }

    } else {
        $data['init'] = true;
    }

//    dump($jscode2session);exit;
    //    dump($follow);exit;
    //    dump($data);exit;
    return $data;
}

function get_wxapp_openid($session)
{
    $opse = get_wxapp_session($session);
    return $opse['openid'];
}

function get_wxapp_uid($session)
{
    $openid = get_wxapp_openid($session);
    return get_uid_by_openid(false, $openid);
}
function get_openid_by_uid($uid, $token)
{

    return M('public_follow')->where(array('uid' => $uid, 'token' => $token))->getField('openid');
}

function get_wxapp_session($session)
{
    if (!$session) {
        return false;
    }

    $tmp = S($session);
    if (!$tmp) {
        $tmp = M('wxapp_code2session')->where(array('session' => $session))->find();
        if (!$tmp) {
            return false;
        }

        return $tmp;
    }
    $opse = json_decode($tmp, true);
    return $opse;
}

function get_newest_sessionkey($session, $openid)
{
    $tmp = M('wxapp_code2session')->where(array('openid' => $openid))->order('id desc')->find();
    if (!$tmp) {
        return false;
    }

    $opse = json_decode($tmp, true);
    S($session, $opse, $tmp['expires_in'] - 200);
}

function router()
{
    $userInfo = get_userinfo(get_mid());
//    dump($userInfo);exit;
    foreach ($userInfo['groups'] as $g) {
//            dump($g);exit;
        if ($g['url']) {
            redirect(U($g['url']));
        }

    }
}

function _deal_products($data)
{
    foreach ($data as &$value) {
        $value['cover'] = get_cover_url($value['cover']);
        $value['cTime'] = time_format($value['cTime']);

    }
    return $data;
}


function _deal_product($data)
{
    $data['cover']    = get_cover_url($data['cover']);
    $head_img         = str2arr($data['head_img']);
    $data['head_img'] = array();
    if ($head_img) {
        foreach ($head_img as $value) {
            $data['head_img'][] = get_cover_url($value, 600); //幻灯片缩略图
        }
    }
    //限时促销
    if ($data['flash_sale']) {
            if ($data['flash_sale_start'] && $data['flash_sale_start'] > NOW_TIME) {
                // 未开始
                $data['flash_sale_timeout'] = 0;
            }
            else if ($data['flash_sale_date'] < NOW_TIME) {
                // 已结束
                $data['flash_sale_timeout'] = 2;
            }else{
                // 正常
                $data['flash_sale_timeout'] = 1;
            }
        }

    return $data;
}


//获取后台商品分类
function get_shop_categories()
{
    $map['token'] = get_token(); //$this->use_token;
    //    $map['city_id'] = get_city_id();
    $list = D('Addons://Shop/Categories')->cgetList($map);
//    dump($list);
    //    echo M()->getLastSql();
    $arr[0] = '无';
    foreach ($list as $vo) {
        $arr[$vo['id']] = $vo['name'];
    }
//    dump($arr);
    return $arr;
}

//获取后台商品规格
function get_product_spec()
{
    $map['token'] = get_token(); //$this->use_token;
    //    $map['city_id'] = get_city_id();
    $list = D('Addons://Shop/Spec')->cgetList($map);
//    dump($list);
    //    echo M()->getLastSql();
    $arr[0] = '无';
    foreach ($list as $vo) {
        $arr[$vo['id']] = $vo['name'];
    }
//    dump($arr);
    return $arr;
}

//获取商品所属的分类
function get_cate_by_id($id)
{
    if (!$id) {
        return false;
    } else {
        $map['id'] = $id;
    }
    $name = M('shop_categories')->where($map)->getField('name');
    return $name;
}


function get_beauty_qrcode($data)
{
    // addWeixinLog(get_token_appinfo());
    $access_token = get_access_token();
    $url          = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=' . $access_token;
    $data         = post_data($url, $data, false, false);
    // dump($data);
    // exit;
    return $data;
}

function getwxacodeunlimit($data)
{
    $access_token = get_access_token();
    $url          = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
    $data         = post_data($url, $data, false, false);
    return $data;
}

function getWxopenTicket($uid)
{
    $map['uid'] = $uid;
        $map['_string'] = '((types=0 and times < 1) or (types=1 and times<3)) and (cTime+86400*7)>' . time();
        $ticket_db      = M('wxopen_ticket');
        // dump($map);
        $ticket         = $ticket_db->where($map)->find();
        // lastsql();exit;
        return $ticket;
}
function addWxopenTicket($form_id, $openid, $types = 0)
{
    if ($form_id == 'the formId is a mock one') {
        return;
    }

    $insert = array(
        'form_id' => $form_id,
        'cTime'   => time(),
        'token'   => get_token(),
        'uid'     => get_uid_by_openid(false, $openid),
        'types'   => $types,
        'openid'  => $openid,
    );
    return M('wxopen_ticket')->add($insert);
}

//0:订单已提交
//1:支付成功
//2:商家已接单
//3:商品配送中
//4:订单完成

function get_order_status($status)
{
    // 0：未支付 1:已支付  2：已发货  3：已完成 4：用户取消订单 5：卖家取消订单 6：系统取消订单 7：退款中 8：已退款 9：已删除
    $status_list = ['未支付', '已支付', '已发货', '已完成', '用户取消订单', '卖家取消订单', '系统取消订单', '退款中', '已退款', '已删除'];

    return $status_list[$status];
}

function get_is_pay_status($status)
{
    switch ($status) {
        case 0:
            return '未支付';
        case 1:
            return '支付成功';
        case 2:
            return '已收款';
    }
}

function get_address($id)
{
    if (empty($id)) {
        return '';
    }

    $address = M('shop_address')->find($id);
    if (!$address) {
        return '地址已删除';
    }

    return $address['name'] . '<br>' . $address['address'] . '<br>' . $address['phone'];
}

function get_shop_name($id)
{
    if (empty($id)) {
        return '';
    }

    return M('shop')->where(array('id' => $id))->getField('name');
}

function get_audit_status($auditid)
{
    $status = M('template_page_history')->where(array('auditid' => $auditid))->getField('status');
    if ($status == 2) {
        $result = D('Addons://Template/Template')->get_audit_status(array('auditid' => $auditid));
        $status = $result['status'];
    }
    switch ($status) {
        case 0:
            return '审核成功';
        case 1:
            return '审核失败';
        case 2:
            return '审核中';
        default:
            return '未知状态';
    }
}

function get_template_name($id)
{
    $title = M('template')->where(array('id' => $id))->getField('title');
    if (!$title) {
        $title = M('template')->where(array('template_id' => $id))->getField('title');
        if ($title) {
            $title .= '/原始方案';
        }

    }
    return $title;
}

function get_ext_json_info($id)
{
    $id = intval($id);
    if (!$id) {
        return '';
    }

    $result = M('template')->where('id=' . $id)->getField('ext_json');
    return $result;
}

function send_template($touser, $tempate_id, $data, $token = '')
{

    $template_data = array(
        'touser'      => $touser,
        'template_id' => $tempate_id,
        'data'        => $data,
    );
    $access_token = get_access_token($token);
    $url          = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
    $result       = post_data($url, $template_data);

    // addWeixinLog($result);

    if ($result['errcode'] == 0) {
        $save['msgid']  = $result['msgid'];
        $save['data']   = json_encode($template_data, true);
        $save['cTime']  = time();
        $save['token']  = $token;
        $save['status'] = 2; // 发送中
        M('sms_template_msg')->add($save);
    }
    return $result;
//    dump($result);
}

function send_wxopen_template($touser, $tempate_id, $data, $token = '')
{

    $template_data = array(
        'touser'      => $touser,
        'template_id' => $tempate_id,
        'data'        => $data,
    );
    $access_token = get_access_token($token);
    $url          = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access_token;
    $result       = post_data($url, $template_data);

    if ($result['errcode'] == 0) {
        $save['msgid']  = $result['msgid'];
        $save['data']   = json_encode($template_data, true);
        $save['cTime']  = time();
        $save['token']  = $token;
        $save['status'] = 2; // 发送中
        M('sms_template_msg')->add($save);
        return true;
    }
    return false;
}

/* 根据id获取file相对路径 */
function get_file_path($id)
{
    if (empty($id)) {
        return false;
    }

    $key  = 'File_' . $id;
    $file = S($key);

    if (!$file) {
        $file = M('File')->where(array(
            'id' => $id,
        ))->find();
        S($key, $file, 86400);
    }

    if (empty($file)) {
        return '';
    }

    $info = $file;

    if ($info['url']) {
        return $info['url'];
    }

    $url = $info['savepath'];
    if (empty($url)) {
        return '';
    }

    return SITE_PATH . '/Uploads/Download/' . $info['savepath'] . $info['savename'];
}

function get_token_appinfo_name($token)
{
    return get_token_appinfo($token, 'public_name');
}

function get_pay_type($payType)
{
    switch ($payType) {
        case 1:
            return '微信支付';
            break;
        case 2:
            return '现金支付';
            break;
        case 3:
            return '货到付款';
            break;
        case 4:
            return '余额支付';
            break;
    }
}
function get_pay_type_name($payType)
{
    switch ($payType) {
        case 1:
            return '微信支付';
            break;
        case 2:
            return '现金支付';
            break;
        case 3:
            return '货到付款';
            break;
        case 4:
            return '余额支付';
            break;
    }
}

function get_pay_status($payType)
{
    switch ($payType) {
        case 0:
            return '未支付';
            break;
        case 1:
            return '已支付';
            break;
        default:
            return '未知状态';
            break;
    }
}

function get_order_detail($detail)
{
    $detail = json_decode($detail, true);
    $html   = '';
    foreach ($detail as $value) {
        $html .= $value['name'] . '  ' . $value['price'] . '  x' . $value['num'] . '<br>';
    }
    return $html;

}

function get_withdraw_status($status)
{
    //0=>申请中, 1 => 审核通过， 2:支付成功, 3:审核不通过， 4：支付失败
    $status_list = ['申请中', '审核通过，支付中', '支付成功', '审核不通过', '支付失败'];
    return $status_list[$status];
}

function error_message($value = '', $errcode = 1)
{
    return array(
        'errcode' => $errcode,
        'errmsg'  => $value,
    );
}

function success_message($value = '', $errcode = 0)
{
    return array(
        'errcode' => $errcode,
        'errmsg'  => $value,
    );
}

function get_retail_status($value='')
{
    $status = ['审核中', '已通过', '未通过'];
    return $status[$value];
}

function get_area_name($id='')
{
    $area = S('area_'.$id);
    if(!$area)
    {
        $area = M('retail_area')->where(array('id' => $id))->getField('name');
        S('area_'.$id, $area);
    }
    return $area;
}

function get_identity_name($id='')
{
    $identity = S('identity_'.$id);
    if(!$identity)
    {
        $identity = M('retail_identity')->where(array('id' => $id))->getField('name');
        S('identity_'.$id, $identity);
    }
    return $identity;
}


 function get_retailor_name($retail_id='')
{
    if(empty($retail_id))
        return false;
    return M('retail_user')->where(array('id' => $retail_id))->getField('name');
}

 function get_product_name($id='')
{
    if(empty($id))
        return false;
    return M('shop_products')->where(array('id' => $id))->getField('name');
}


 
/**
 *  post提交数据 
 * @param  string $url 请求Url
 * @param  array $datas 提交的数据 
 * @return url响应返回的html
 */
function sendPost($url, $datas) {
    $temps = array();   
    foreach ($datas as $key => $value) {
        $temps[] = sprintf('%s=%s', $key, $value);      
    }   
    $post_data = implode('&', $temps);
    $url_info = parse_url($url);
    if(empty($url_info['port']))
    {
        $url_info['port']=80;   
    }
    $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
    $httpheader.= "Host:" . $url_info['host'] . "\r\n";
    $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
    $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
    $httpheader.= "Connection:close\r\n\r\n";
    $httpheader.= $post_data;
    $fd = fsockopen($url_info['host'], $url_info['port']);
    fwrite($fd, $httpheader);
    $gets = "";
    $headerFlag = true;
    while (!feof($fd)) {
        if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
            break;
        }
    }
    while (!feof($fd)) {
        $gets.= fread($fd, 128);
    }
    fclose($fd);  
    
    return $gets;
}

/**
 * 电商Sign签名生成
 * @param data 内容   
 * @param appkey Appkey
 * @return DataSign签名
 */
function encrypt($data, $appkey) {
    return urlencode(base64_encode(md5($data.$appkey)));
}

function getOrderTracesByJsonCode($data = array()){
   if(!count($data))
        return;
    $requestData= json_encode($data, JSON_UNESCAPED_UNICODE);
    // echo $requestData;
    // echo EBusinessID;
    // echo AppKey;
    // $requestData= "{'OrderCode':'','ShipperCode':'YTO','LogisticCode':'12345678'}";
    $datas = array(
        'EBusinessID' => EBusinessID,
        'RequestType' => '1002',
        'RequestData' => urlencode($requestData) ,
        'DataType' => '2',
    );
    $datas['DataSign'] = urlencode(base64_encode(md5($requestData.AppKey))) ;
    $result=sendPost(ReqURL, $datas);   
    
    //根据公司业务处理返回的信息......
    
    return $result;
}


function getOrderTracesByJson(){
    $requestData= "{'LogisticCode':'1000745320654'}";
    $datas = array(
        'EBusinessID' => EBusinessID,
        'RequestType' => '2002',
        'RequestData' => urlencode($requestData) ,
        'DataType' => '2',
    );
    $datas['DataSign'] = encrypt($requestData, AppKey);
    $result=sendPost(ReqURL, $datas);   
    
    //根据公司业务处理返回的信息......
    
    return $result;
}



function get_repair_type_name($id)
{
    return M('repair_type')->where(array('id' => $id))->getField('name');
}


function get_repair_location_name($id)
{
    return M('repair_list')->where(array('id' => $id))->getField('name');
}

function get_reserve_detail($id)
{
            $detail = M('reserve_order_detail a')->join('wp_reserve_rule b  on a.rule_id = b.id')->join('wp_reserve_list c on a.reserve_id = c.id')->field('a.*,b.title, b.starttime, b.endtime, c.name, c.cover')->where('order_id="' . $id . '"')->select();
            $str = '';
            foreach ($detail as  $value) {
                $time = date('H:i', $value['starttime']).'-'.date('H:i', $value['endtime']);
                $str .= $value['name'].'/'.$value['title'].'/'.$time;
            }
            return $str;
}

function send_email($to,$title,$content){

    //实例化PHPMailer核心类
    $mail = new PHPMailer(true);

    //是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式
    $mail->SMTPDebug = 1;

    //使用smtp鉴权方式发送邮件
    $mail->isSMTP();

    //smtp需要鉴权 这个必须是true
    $mail->SMTPAuth=true;

    //链接qq域名邮箱的服务器地址
    $mail->Host = 'smtp.qq.com';

    //设置使用ssl加密方式登录鉴权
    $mail->SMTPSecure = 'ssl';

    //设置ssl连接smtp服务器的远程服务器端口号，以前的默认是25，但是现在新的好像已经不可用了 可选465或587
    $mail->Port = 465;

    //设置smtp的helo消息头 这个可有可无 内容任意
    // $mail->Helo = 'Hello smtp.qq.com Server';

    //设置发件人的主机域 可有可无 默认为localhost 内容任意，建议使用你的域名
    $mail->Hostname = 'www.book.com';

    //设置发送的邮件的编码 可选GB2312 我喜欢utf-8 据说utf8在某些客户端收信下会乱码
    $mail->CharSet = 'UTF-8';

    //设置发件人姓名（昵称） 任意内容，显示在收件人邮件的发件人邮箱地址前的发件人姓名
    $mail->FromName = '刻意练习';

    //smtp登录的账号 这里填入字符串格式的qq号即可
    $mail->Username ='1269886839';

    //smtp登录的密码 使用生成的授权码（就刚才叫你保存的最新的授权码）
    $mail->Password = 'fqyqtkqbdcfjifde';

    //设置发件人邮箱地址 这里填入上述提到的“发件人邮箱”
    $mail->From = '1269886839@qq.com';

    //邮件正文是否为html编码 注意此处是一个方法 不再是属性 true或false
    $mail->isHTML(true); 

    //设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大
    $mail->addAddress($to,'lsgo在线通知');

    //添加多个收件人 则多次调用方法即可
    // $mail->addAddress('xxx@163.com','lsgo在线通知');

    //添加该邮件的主题
    $mail->Subject = $title;

    //添加邮件正文 上方将isHTML设置成了true，则可以是完整的html字符串 如：使用file_get_contents函数读取本地的html文件
    $mail->Body = $content;

    //为该邮件添加附件 该方法也有两个参数 第一个参数为附件存放的目录（相对目录、或绝对目录均可） 第二参数为在邮件附件中该附件的名称
    // $mail->addAttachment('./d.jpg','mm.jpg');
    //同样该方法可以多次调用 上传多个附件
    // $mail->addAttachment('./Jlib-1.1.0.js','Jlib.js');

    $status = $mail->send();

    //简单的判断与提示信息
    if($status) {
        return true;
    }else{
        return false;
    }
  }

  // 格式化日期
function parse_date($data, $parse="Y-m-d H:i") {
  foreach ($data as $key => &$value) {
    if($value['cTime']) {
      $value['cTime'] = date($parse, $value['cTime']);
    }

    if($value['post_date']) {
        $value['post_date'] = date($parse, $value['post_date']);
    }

    if($value['post_modified']) {
        $value['post_modified'] = date($parse, $value['post_modified']);
    }

  }
  return $data;
}


// 获取图片神技能
function get__url($data) {

 if(gettype($data) == 'array') {
   foreach($data as $k => &$v){
       if($v['cover'] && $v['cover'] != 0) {
           $v['cover'] = get_cover_url($v['cover']);
       }

       if($v['slide_pic'] && $v['slide_pic']) {
            $v['cover'] = get_cover_url($v['slide_pic']);
       }

       if($v['thumb'] && $v['thumb']) {
            $v['cover'] = get_cover_url($v['thumb']);
       }


       if($v['banner'] && $v['banner']) {
            $v['banner'] = get_cover_url($v['banner']);
       }

       
       if($v['photos']) {
           $v['photos'] = explode(',', $v['photos']);
           foreach($v['photos'] as $k => &$v1) {
             $v1 = get_cover_url($v1);
           }
       }
   }
 }

 return $data;
}










